PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 * ereport(FATAL). Emergency termination is by SIGQUIT; like any postmaster
34 * child process, the walreceiver will simply abort and exit on SIGQUIT. A
35 * close 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-2026, 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 "access/xlogwait.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"
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#include "utils/wait_event.h"
83
84
85/*
86 * GUC variables. (Other variables that affect walreceiver are in xlog.c
87 * because they're passed down from the startup process, for better
88 * synchronization.)
89 */
93
94/* libpqwalreceiver connection */
97
98/*
99 * These variables are used similarly to openLogFile/SegNo,
100 * but for walreceiver to write the XLOG. recvFileTLI is the TimeLineID
101 * corresponding the filename of recvFile.
102 */
103static int recvFile = -1;
106
107/*
108 * LogstreamResult indicates the byte positions that we have already
109 * written/fsynced.
110 */
111static struct
112{
113 XLogRecPtr Write; /* last byte + 1 written out in the standby */
114 XLogRecPtr Flush; /* last byte + 1 flushed in the standby */
116
117/*
118 * Reasons to wake up and perform periodic tasks.
119 */
128
129/*
130 * Wake up times for periodic tasks.
131 */
133
135
136/* Prototypes for private functions */
138static void WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI);
139static void WalRcvDie(int code, Datum arg);
140static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len,
141 TimeLineID tli);
142static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr,
143 TimeLineID tli);
144static void XLogWalRcvFlush(bool dying, TimeLineID tli);
146static void XLogWalRcvSendReply(bool force, bool requestReply, bool checkApply);
147static void XLogWalRcvSendHSFeedback(bool immed);
150
151
152/* Main entry point for walreceiver process */
153void
155{
156 char conninfo[MAXCONNINFO];
157 char *tmp_conninfo;
158 char slotname[NAMEDATALEN];
159 bool is_temp_slot;
160 XLogRecPtr startpoint;
161 TimeLineID startpointTLI;
163 bool first_stream;
166 char *err;
167 char *sender_host = NULL;
168 int sender_port = 0;
169 char *appname;
170
172
174
175 /*
176 * WalRcv should be set up already (if we are a backend, we inherit this
177 * by fork() or EXEC_BACKEND mechanism from the postmaster).
178 */
179 walrcv = WalRcv;
180 Assert(walrcv != NULL);
181
182 /*
183 * Mark walreceiver as running in shared memory.
184 *
185 * Do this as early as possible, so that if we fail later on, we'll set
186 * state to STOPPED. If we die before this, the startup process will keep
187 * waiting for us to start up, until it times out.
188 */
189 SpinLockAcquire(&walrcv->mutex);
190 Assert(walrcv->pid == 0);
191 switch (walrcv->walRcvState)
192 {
193 case WALRCV_STOPPING:
194 /* If we've already been requested to stop, don't start up. */
195 walrcv->walRcvState = WALRCV_STOPPED;
197
198 case WALRCV_STOPPED:
199 SpinLockRelease(&walrcv->mutex);
200 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
201 proc_exit(1);
202 break;
203
204 case WALRCV_STARTING:
205 /* The usual case */
206 break;
207
209 case WALRCV_WAITING:
210 case WALRCV_STREAMING:
212 default:
213 /* Shouldn't happen */
214 SpinLockRelease(&walrcv->mutex);
215 elog(PANIC, "walreceiver still running according to shared memory state");
216 }
217 /* Advertise our PID so that the startup process can kill us */
218 walrcv->pid = MyProcPid;
219 walrcv->walRcvState = WALRCV_CONNECTING;
220
221 /* Fetch information required to start streaming */
222 walrcv->ready_to_display = false;
223 strlcpy(conninfo, walrcv->conninfo, MAXCONNINFO);
224 strlcpy(slotname, walrcv->slotname, NAMEDATALEN);
225 is_temp_slot = walrcv->is_temp_slot;
226 startpoint = walrcv->receiveStart;
227 startpointTLI = walrcv->receiveStartTLI;
228
229 /*
230 * At most one of is_temp_slot and slotname can be set; otherwise,
231 * RequestXLogStreaming messed up.
232 */
233 Assert(!is_temp_slot || (slotname[0] == '\0'));
234
235 /* Initialise to a sanish value */
237 walrcv->lastMsgSendTime =
238 walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now;
239
240 /* Report our proc number so that others can wake us up */
241 walrcv->procno = MyProcNumber;
242
243 SpinLockRelease(&walrcv->mutex);
244
245 /* Arrange to clean up at walreceiver exit */
246 on_shmem_exit(WalRcvDie, PointerGetDatum(&startpointTLI));
247
248 /* Properly accept or ignore signals the postmaster might send us */
249 pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config
250 * file */
252 pqsignal(SIGTERM, die); /* request shutdown */
253 /* SIGQUIT handler was already set up by InitPostmasterChild */
258
259 /* Reset some signals that are accepted by postmaster but not here */
261
262 /* Load the libpq-specific functions */
263 load_file("libpqwalreceiver", false);
265 elog(ERROR, "libpqwalreceiver didn't initialize correctly");
266
267 /* Unblock signals (they were blocked when the postmaster forked us) */
269
270 /* Establish the connection to the primary for XLOG streaming */
271 appname = cluster_name[0] ? cluster_name : "walreceiver";
272 wrconn = walrcv_connect(conninfo, true, false, false, appname, &err);
273 if (!wrconn)
276 errmsg("streaming replication receiver \"%s\" could not connect to the primary server: %s",
277 appname, err)));
278
279 /*
280 * Save user-visible connection string. This clobbers the original
281 * conninfo, for security. Also save host and port of the sender server
282 * this walreceiver is connected to.
283 */
285 walrcv_get_senderinfo(wrconn, &sender_host, &sender_port);
286 SpinLockAcquire(&walrcv->mutex);
287 memset(walrcv->conninfo, 0, MAXCONNINFO);
288 if (tmp_conninfo)
290
291 memset(walrcv->sender_host, 0, NI_MAXHOST);
292 if (sender_host)
293 strlcpy(walrcv->sender_host, sender_host, NI_MAXHOST);
294
295 walrcv->sender_port = sender_port;
296 walrcv->ready_to_display = true;
297 SpinLockRelease(&walrcv->mutex);
298
299 if (tmp_conninfo)
301
302 if (sender_host)
303 pfree(sender_host);
304
305 /* Initialize buffers for processing messages */
307
308 first_stream = true;
309 for (;;)
310 {
311 char *primary_sysid;
312 char standby_sysid[32];
314
315 /*
316 * Check that we're connected to a valid server using the
317 * IDENTIFY_SYSTEM replication command.
318 */
320
324 {
327 errmsg("database system identifier differs between the primary and standby"),
328 errdetail("The primary's identifier is %s, the standby's identifier is %s.",
330 }
332
333 /*
334 * Confirm that the current timeline of the primary is the same or
335 * ahead of ours.
336 */
337 if (primaryTLI < startpointTLI)
340 errmsg("highest timeline %u of the primary is behind recovery timeline %u",
341 primaryTLI, startpointTLI)));
342
343 /*
344 * Get any missing history files. We do this always, even when we're
345 * not interested in that timeline, so that if we're promoted to
346 * become the primary later on, we don't select the same timeline that
347 * was already used in the current primary. This isn't bullet-proof -
348 * you'll need some external software to manage your cluster if you
349 * need to ensure that a unique timeline id is chosen in every case,
350 * but let's avoid the confusion of timeline id collisions where we
351 * can.
352 */
354
355 /*
356 * Create temporary replication slot if requested, and update slot
357 * name in shared memory. (Note the slot name cannot already be set
358 * in this case.)
359 */
360 if (is_temp_slot)
361 {
362 snprintf(slotname, sizeof(slotname),
363 "pg_walreceiver_%lld",
364 (long long int) walrcv_get_backend_pid(wrconn));
365
366 walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL);
367
368 SpinLockAcquire(&walrcv->mutex);
369 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
370 SpinLockRelease(&walrcv->mutex);
371 }
372
373 /*
374 * Start streaming.
375 *
376 * We'll try to start at the requested starting point and timeline,
377 * even if it's different from the server's latest timeline. In case
378 * we've already reached the end of the old timeline, the server will
379 * finish the streaming immediately, and we will go back to await
380 * orders from the startup process. If recovery_target_timeline is
381 * 'latest', the startup process will scan pg_wal and find the new
382 * history file, bump recovery target timeline, and ask us to restart
383 * on the new timeline.
384 */
385 options.logical = false;
386 options.startpoint = startpoint;
387 options.slotname = slotname[0] != '\0' ? slotname : NULL;
388 options.proto.physical.startpointTLI = startpointTLI;
390 {
391 if (first_stream)
392 ereport(LOG,
393 errmsg("started streaming WAL from primary at %X/%08X on timeline %u",
394 LSN_FORMAT_ARGS(startpoint), startpointTLI));
395 else
396 ereport(LOG,
397 errmsg("restarted WAL streaming at %X/%08X on timeline %u",
398 LSN_FORMAT_ARGS(startpoint), startpointTLI));
399 first_stream = false;
400
401 /*
402 * Switch to STREAMING after a successful connection if current
403 * state is CONNECTING. This switch happens after an initial
404 * startup, or after a restart as determined by
405 * WalRcvWaitForStartPosition().
406 */
407 SpinLockAcquire(&walrcv->mutex);
408 if (walrcv->walRcvState == WALRCV_CONNECTING)
409 walrcv->walRcvState = WALRCV_STREAMING;
410 SpinLockRelease(&walrcv->mutex);
411
412 /* Initialize LogstreamResult for processing messages */
414
415 /* Initialize nap wakeup times. */
417 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
419
420 /* Send initial reply/feedback messages. */
421 XLogWalRcvSendReply(true, false, false);
423
424 /* Loop until end-of-streaming or error */
425 for (;;)
426 {
427 char *buf;
428 int len;
429 bool endofwal = false;
431 int rc;
433 long nap;
434
435 /*
436 * Exit walreceiver if we're not in recovery. This should not
437 * happen, but cross-check the status here.
438 */
439 if (!RecoveryInProgress())
442 errmsg("cannot continue WAL streaming, recovery has already ended")));
443
444 /* Process any requests or signals received recently */
446
448 {
449 ConfigReloadPending = false;
451 /* recompute wakeup times */
453 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
456 }
457
458 /* See if we can read data immediately */
460 if (len != 0)
461 {
462 /*
463 * Process the received data, and any subsequent data we
464 * can read without blocking.
465 */
466 for (;;)
467 {
468 if (len > 0)
469 {
470 /*
471 * Something was received from primary, so adjust
472 * the ping and terminate wakeup times.
473 */
476 now);
478 XLogWalRcvProcessMsg(buf[0], &buf[1], len - 1,
479 startpointTLI);
480 }
481 else if (len == 0)
482 break;
483 else if (len < 0)
484 {
485 ereport(LOG,
486 (errmsg("replication terminated by primary server"),
487 errdetail("End of WAL reached on timeline %u at %X/%08X.",
488 startpointTLI,
490 endofwal = true;
491 break;
492 }
494 }
495
496 /* Let the primary know that we received some data. */
497 XLogWalRcvSendReply(false, false, false);
498
499 /*
500 * If we've written some records, flush them to disk and
501 * let the startup process and primary server know about
502 * them.
503 */
504 XLogWalRcvFlush(false, startpointTLI);
505 }
506
507 /* Check if we need to exit the streaming loop. */
508 if (endofwal)
509 break;
510
511 /* Find the soonest wakeup time, to limit our nap. */
513 for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
515
516 /* Calculate the nap time, clamping as necessary. */
519
520 /*
521 * Ideally we would reuse a WaitEventSet object repeatedly
522 * here to avoid the overheads of WaitLatchOrSocket on epoll
523 * systems, but we can't be sure that libpq (or any other
524 * walreceiver implementation) has the same socket (even if
525 * the fd is the same number, it may have been closed and
526 * reopened since the last time). In future, if there is a
527 * function for removing sockets from WaitEventSet, then we
528 * could add and remove just the socket each time, potentially
529 * avoiding some system calls.
530 */
535 wait_fd,
536 nap,
538 if (rc & WL_LATCH_SET)
539 {
542
543 if (walrcv->apply_reply_requested)
544 {
545 /*
546 * The recovery process has asked us to send apply
547 * feedback now. Make sure the flag is really set to
548 * false in shared memory before sending the reply, so
549 * we don't miss a new request for a reply.
550 */
551 walrcv->apply_reply_requested = false;
553 XLogWalRcvSendReply(false, false, true);
554 }
555 }
556 if (rc & WL_TIMEOUT)
557 {
558 /*
559 * We didn't receive anything new. If we haven't heard
560 * anything from the server for more than
561 * wal_receiver_timeout / 2, ping the server. Also, if
562 * it's been longer than wal_receiver_status_interval
563 * since the last update we sent, send a status update to
564 * the primary anyway, to report any progress in applying
565 * WAL.
566 */
567 bool requestReply = false;
568
569 /*
570 * Report pending statistics to the cumulative stats
571 * system. This location is useful for the report as it
572 * is not within a tight loop in the WAL receiver, to
573 * avoid bloating pgstats with requests, while also making
574 * sure that the reports happen each time a status update
575 * is sent.
576 */
577 pgstat_report_wal(false);
578
579 /*
580 * Check if time since last receive from primary has
581 * reached the configured limit.
582 */
587 errmsg("terminating walreceiver due to timeout")));
588
589 /*
590 * If we didn't receive anything new for half of receiver
591 * replication timeout, then ping the server.
592 */
594 {
595 requestReply = true;
597 }
598
601 }
602 }
603
604 /*
605 * The backend finished streaming. Exit streaming COPY-mode from
606 * our side, too.
607 */
609
610 /*
611 * If the server had switched to a new timeline that we didn't
612 * know about when we began streaming, fetch its timeline history
613 * file now.
614 */
616 }
617 else
618 ereport(LOG,
619 (errmsg("primary server contains no more WAL on requested timeline %u",
620 startpointTLI)));
621
622 /*
623 * End of WAL reached on the requested timeline. Close the last
624 * segment, and await for new orders from the startup process.
625 */
626 if (recvFile >= 0)
627 {
629
630 XLogWalRcvFlush(false, startpointTLI);
632 if (close(recvFile) != 0)
635 errmsg("could not close WAL segment %s: %m",
636 xlogfname)));
637
638 /*
639 * Create .done file forcibly to prevent the streamed segment from
640 * being archived later.
641 */
644 else
646 }
647 recvFile = -1;
648
649 elog(DEBUG1, "walreceiver ended streaming and awaits new instructions");
650 WalRcvWaitForStartPosition(&startpoint, &startpointTLI);
651 }
652 /* not reached */
653}
654
655/*
656 * Wait for startup process to set receiveStart and receiveStartTLI.
657 */
658static void
660{
662 int state;
663
664 SpinLockAcquire(&walrcv->mutex);
665 state = walrcv->walRcvState;
667 {
668 SpinLockRelease(&walrcv->mutex);
669 if (state == WALRCV_STOPPING)
670 proc_exit(0);
671 else
672 elog(FATAL, "unexpected walreceiver state");
673 }
674 walrcv->walRcvState = WALRCV_WAITING;
675 walrcv->receiveStart = InvalidXLogRecPtr;
676 walrcv->receiveStartTLI = 0;
677 SpinLockRelease(&walrcv->mutex);
678
679 set_ps_display("idle");
680
681 /*
682 * nudge startup process to notice that we've stopped streaming and are
683 * now waiting for instructions.
684 */
686 for (;;)
687 {
689
691
692 SpinLockAcquire(&walrcv->mutex);
693 Assert(walrcv->walRcvState == WALRCV_RESTARTING ||
694 walrcv->walRcvState == WALRCV_WAITING ||
695 walrcv->walRcvState == WALRCV_STOPPING);
696 if (walrcv->walRcvState == WALRCV_RESTARTING)
697 {
698 /*
699 * No need to handle changes in primary_conninfo or
700 * primary_slot_name here. Startup process will signal us to
701 * terminate in case those change.
702 */
703 *startpoint = walrcv->receiveStart;
704 *startpointTLI = walrcv->receiveStartTLI;
705 walrcv->walRcvState = WALRCV_CONNECTING;
706 SpinLockRelease(&walrcv->mutex);
707 break;
708 }
709 if (walrcv->walRcvState == WALRCV_STOPPING)
710 {
711 /*
712 * We should've received SIGTERM if the startup process wants us
713 * to die, but might as well check it here too.
714 */
715 SpinLockRelease(&walrcv->mutex);
716 proc_exit(1);
717 }
718 SpinLockRelease(&walrcv->mutex);
719
722 }
723
725 {
726 char activitymsg[50];
727
728 snprintf(activitymsg, sizeof(activitymsg), "restarting at %X/%08X",
729 LSN_FORMAT_ARGS(*startpoint));
731 }
732}
733
734/*
735 * Fetch any missing timeline history files between 'first' and 'last'
736 * (inclusive) from the server.
737 */
738static void
740{
741 TimeLineID tli;
742
743 for (tli = first; tli <= last; tli++)
744 {
745 /* there's no history file for timeline 1 */
746 if (tli != 1 && !existsTimeLineHistory(tli))
747 {
748 char *fname;
749 char *content;
750 int len;
752
753 ereport(LOG,
754 (errmsg("fetching timeline history file for timeline %u from primary server",
755 tli)));
756
757 walrcv_readtimelinehistoryfile(wrconn, tli, &fname, &content, &len);
758
759 /*
760 * Check that the filename on the primary matches what we
761 * calculated ourselves. This is just a sanity check, it should
762 * always match.
763 */
765 if (strcmp(fname, expectedfname) != 0)
768 errmsg_internal("primary reported unexpected file name for timeline history file of timeline %u",
769 tli)));
770
771 /*
772 * Write the file to pg_wal.
773 */
774 writeTimeLineHistoryFile(tli, content, len);
775
776 /*
777 * Mark the streamed history file as ready for archiving if
778 * archive_mode is always.
779 */
782 else
783 XLogArchiveNotify(fname);
784
785 pfree(fname);
786 pfree(content);
787 }
788 }
789}
790
791/*
792 * Mark us as STOPPED in shared memory at exit.
793 */
794static void
796{
799
800 Assert(*startpointTLI_p != 0);
801
802 /* Ensure that all WAL records received are flushed to disk */
804
805 /* Mark ourselves inactive in shared memory */
806 SpinLockAcquire(&walrcv->mutex);
807 Assert(walrcv->walRcvState == WALRCV_STREAMING ||
808 walrcv->walRcvState == WALRCV_CONNECTING ||
809 walrcv->walRcvState == WALRCV_RESTARTING ||
810 walrcv->walRcvState == WALRCV_STARTING ||
811 walrcv->walRcvState == WALRCV_WAITING ||
812 walrcv->walRcvState == WALRCV_STOPPING);
813 Assert(walrcv->pid == MyProcPid);
814 walrcv->walRcvState = WALRCV_STOPPED;
815 walrcv->pid = 0;
816 walrcv->procno = INVALID_PROC_NUMBER;
817 walrcv->ready_to_display = false;
818 SpinLockRelease(&walrcv->mutex);
819
820 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
821
822 /* Terminate the connection gracefully. */
823 if (wrconn != NULL)
825
826 /* Wake up the startup process to notice promptly that we're gone */
828}
829
830/*
831 * Accept the message from XLOG stream, and process it.
832 */
833static void
834XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
835{
836 int hdrlen;
840 bool replyRequested;
841
842 switch (type)
843 {
845 {
847
848 hdrlen = sizeof(int64) + sizeof(int64) + sizeof(int64);
849 if (len < hdrlen)
852 errmsg_internal("invalid WAL message received from primary")));
853
854 /* initialize a StringInfo with the given buffer */
856
857 /* read the fields */
862
863 buf += hdrlen;
864 len -= hdrlen;
866 break;
867 }
869 {
871
872 hdrlen = sizeof(int64) + sizeof(int64) + sizeof(char);
873 if (len != hdrlen)
876 errmsg_internal("invalid keepalive message received from primary")));
877
878 /* initialize a StringInfo with the given buffer */
880
881 /* read the fields */
885
887
888 /* If the primary requested a reply, send one immediately */
889 if (replyRequested)
890 XLogWalRcvSendReply(true, false, false);
891 break;
892 }
893 default:
896 errmsg_internal("invalid replication message type %d",
897 type)));
898 }
899}
900
901/*
902 * Write XLOG data to disk.
903 */
904static void
906{
907 int startoff;
908 int byteswritten;
910
911 Assert(tli != 0);
912
913 while (nbytes > 0)
914 {
915 int segbytes;
916
917 /* Close the current segment if it's completed */
920
921 if (recvFile < 0)
922 {
923 /* Create/use new log file */
926 recvFileTLI = tli;
927 }
928
929 /* Calculate the start offset of the received logs */
931
932 if (startoff + nbytes > wal_segment_size)
934 else
935 segbytes = nbytes;
936
937 /* OK to write the logs */
938 errno = 0;
939
940 /*
941 * Measure I/O timing to write WAL data, for pg_stat_io.
942 */
944
948
951
952 if (byteswritten <= 0)
953 {
955 int save_errno;
956
957 /* if write didn't set errno, assume no disk space */
958 if (errno == 0)
959 errno = ENOSPC;
960
966 errmsg("could not write to WAL segment %s "
967 "at offset %d, length %d: %m",
969 }
970
971 /* Update state for write */
973
974 nbytes -= byteswritten;
975 buf += byteswritten;
976
977 LogstreamResult.Write = recptr;
978 }
979
980 /* Update shared-memory status */
982
983 /*
984 * If we wrote an LSN that someone was waiting for, notify the waiters.
985 */
986 if (waitLSNState &&
987 (LogstreamResult.Write >=
990
991 /*
992 * Close the current segment if it's fully written up in the last cycle of
993 * the loop, to create its archive notification file soon. Otherwise WAL
994 * archiving of the segment will be delayed until any data in the next
995 * segment is received and written.
996 */
999}
1000
1001/*
1002 * Flush the log to disk.
1003 *
1004 * If we're in the midst of dying, it's unwise to do anything that might throw
1005 * an error, so we skip sending a reply in that case.
1006 */
1007static void
1009{
1010 Assert(tli != 0);
1011
1012 if (LogstreamResult.Flush < LogstreamResult.Write)
1013 {
1015
1017
1018 LogstreamResult.Flush = LogstreamResult.Write;
1019
1020 /* Update shared-memory status */
1021 SpinLockAcquire(&walrcv->mutex);
1022 if (walrcv->flushedUpto < LogstreamResult.Flush)
1023 {
1024 walrcv->latestChunkStart = walrcv->flushedUpto;
1025 walrcv->flushedUpto = LogstreamResult.Flush;
1026 walrcv->receivedTLI = tli;
1027 }
1028 SpinLockRelease(&walrcv->mutex);
1029
1030 /*
1031 * If we flushed an LSN that someone was waiting for, notify the
1032 * waiters.
1033 */
1034 if (waitLSNState &&
1035 (LogstreamResult.Flush >=
1038
1039 /* Signal the startup process and walsender that new WAL has arrived */
1042 WalSndWakeup(true, false);
1043
1044 /* Report XLOG streaming progress in PS display */
1046 {
1047 char activitymsg[50];
1048
1049 snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%08X",
1052 }
1053
1054 /* Also let the primary know that we made some progress */
1055 if (!dying)
1056 {
1057 XLogWalRcvSendReply(false, false, false);
1059 }
1060 }
1061}
1062
1063/*
1064 * Close the current segment.
1065 *
1066 * Flush the segment to disk before closing it. Otherwise we have to
1067 * reopen and fsync it later.
1068 *
1069 * Create an archive notification file since the segment is known completed.
1070 */
1071static void
1073{
1074 char xlogfname[MAXFNAMELEN];
1075
1077 Assert(tli != 0);
1078
1079 /*
1080 * fsync() and close current file before we switch to next one. We would
1081 * otherwise have to reopen this file to fsync it later
1082 */
1083 XLogWalRcvFlush(false, tli);
1084
1086
1087 /*
1088 * XLOG segment files will be re-read by recovery in startup process soon,
1089 * so we don't advise the OS to release cache pages associated with the
1090 * file like XLogFileClose() does.
1091 */
1092 if (close(recvFile) != 0)
1093 ereport(PANIC,
1095 errmsg("could not close WAL segment %s: %m",
1096 xlogfname)));
1097
1098 /*
1099 * Create .done file forcibly to prevent the streamed segment from being
1100 * archived later.
1101 */
1104 else
1106
1107 recvFile = -1;
1108}
1109
1110/*
1111 * Send reply message to primary, indicating our current WAL locations and
1112 * time.
1113 *
1114 * The message is sent if 'force' is set, if enough time has passed since the
1115 * last update to reach wal_receiver_status_interval, or if WAL locations have
1116 * advanced since the previous status update. If wal_receiver_status_interval
1117 * is disabled and 'force' is false, this function does nothing. Set 'force' to
1118 * send the message unconditionally.
1119 *
1120 * Whether WAL locations are considered "advanced" depends on 'checkApply'.
1121 * If 'checkApply' is false, only the write and flush locations are checked.
1122 * This should be used when the call is triggered by write/flush activity
1123 * (e.g., after walreceiver writes or flushes WAL), and avoids the
1124 * apply-location check, which requires a spinlock. If 'checkApply' is true,
1125 * the apply location is also considered. This should be used when the apply
1126 * location is expected to advance (e.g., when the startup process requests
1127 * an apply notification).
1128 *
1129 * If 'requestReply' is true, requests the server to reply immediately upon
1130 * receiving this message. This is used for heartbeats, when approaching
1131 * wal_receiver_timeout.
1132 */
1133static void
1135{
1141
1142 /*
1143 * If the user doesn't want status to be reported to the primary, be sure
1144 * to exit before doing anything at all.
1145 */
1146 if (!force && wal_receiver_status_interval <= 0)
1147 return;
1148
1149 /* Get current timestamp. */
1151
1152 /*
1153 * We can compare the write and flush positions to the last message we
1154 * sent without taking any lock, but the apply position requires a spin
1155 * lock, so we don't check that unless it is expected to advance since the
1156 * previous update, i.e., when 'checkApply' is true.
1157 */
1158 if (!force && now < wakeup[WALRCV_WAKEUP_REPLY])
1159 {
1160 if (checkApply)
1162
1163 if (writePtr == LogstreamResult.Write
1164 && flushPtr == LogstreamResult.Flush
1166 return;
1167 }
1168
1169 /* Make sure we wake up when it's time to send another reply. */
1171
1172 /* Construct a new message */
1173 writePtr = LogstreamResult.Write;
1174 flushPtr = LogstreamResult.Flush;
1177
1185
1186 /* Send it */
1187 elog(DEBUG2, "sending write %X/%08X flush %X/%08X apply %X/%08X%s",
1191 requestReply ? " (reply requested)" : "");
1192
1194}
1195
1196/*
1197 * Send hot standby feedback message to primary, plus the current time,
1198 * in case they don't have a watch.
1199 *
1200 * If the user disables feedback, send one final message to tell sender
1201 * to forget about the xmin on this standby. We also send this message
1202 * on first connect because a previous connection might have set xmin
1203 * on a replication slot. (If we're not using a slot it's harmless to
1204 * send a feedback message explicitly setting InvalidTransactionId).
1205 */
1206static void
1208{
1211 TransactionId nextXid;
1214 TransactionId xmin,
1215 catalog_xmin;
1216
1217 /* initially true so we always send at least one feedback message */
1218 static bool primary_has_standby_xmin = true;
1219
1220 /*
1221 * If the user doesn't want status to be reported to the primary, be sure
1222 * to exit before doing anything at all.
1223 */
1226 return;
1227
1228 /* Get current timestamp. */
1230
1231 /* Send feedback at most once per wal_receiver_status_interval. */
1233 return;
1234
1235 /* Make sure we wake up when it's time to send feedback again. */
1237
1238 /*
1239 * If Hot Standby is not yet accepting connections there is nothing to
1240 * send. Check this after the interval has expired to reduce number of
1241 * calls.
1242 *
1243 * Bailing out here also ensures that we don't send feedback until we've
1244 * read our own replication slot state, so we don't tell the primary to
1245 * discard needed xmin or catalog_xmin from any slots that may exist on
1246 * this replica.
1247 */
1248 if (!HotStandbyActive())
1249 return;
1250
1251 /*
1252 * Make the expensive call to get the oldest xmin once we are certain
1253 * everything else has been checked.
1254 */
1256 {
1257 GetReplicationHorizons(&xmin, &catalog_xmin);
1258 }
1259 else
1260 {
1261 xmin = InvalidTransactionId;
1262 catalog_xmin = InvalidTransactionId;
1263 }
1264
1265 /*
1266 * Get epoch and adjust if nextXid and oldestXmin are different sides of
1267 * the epoch boundary.
1268 */
1273 if (nextXid < xmin)
1274 xmin_epoch--;
1275 if (nextXid < catalog_xmin)
1277
1278 elog(DEBUG2, "sending hot standby feedback xmin %u epoch %u catalog_xmin %u catalog_xmin_epoch %u",
1279 xmin, xmin_epoch, catalog_xmin, catalog_xmin_epoch);
1280
1281 /* Construct the message and send it. */
1287 pq_sendint32(&reply_message, catalog_xmin);
1290 if (TransactionIdIsValid(xmin) || TransactionIdIsValid(catalog_xmin))
1292 else
1294}
1295
1296/*
1297 * Update shared memory status upon receiving a message from primary.
1298 *
1299 * 'walEnd' and 'sendTime' are the end-of-WAL and timestamp of the latest
1300 * message, reported by primary.
1301 */
1302static void
1304{
1306 TimestampTz lastMsgReceiptTime = GetCurrentTimestamp();
1307
1308 /* Update shared-memory status */
1309 SpinLockAcquire(&walrcv->mutex);
1310 if (walrcv->latestWalEnd < walEnd)
1311 walrcv->latestWalEndTime = sendTime;
1312 walrcv->latestWalEnd = walEnd;
1313 walrcv->lastMsgSendTime = sendTime;
1314 walrcv->lastMsgReceiptTime = lastMsgReceiptTime;
1315 SpinLockRelease(&walrcv->mutex);
1316
1318 {
1319 char *sendtime;
1320 char *receipttime;
1321 int applyDelay;
1322
1323 /* Copy because timestamptz_to_str returns a static buffer */
1325 receipttime = pstrdup(timestamptz_to_str(lastMsgReceiptTime));
1327
1328 /* apply delay is not available */
1329 if (applyDelay == -1)
1330 elog(DEBUG2, "sendtime %s receipttime %s replication apply delay (N/A) transfer latency %d ms",
1331 sendtime,
1334 else
1335 elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
1336 sendtime,
1338 applyDelay,
1340
1341 pfree(sendtime);
1343 }
1344}
1345
1346/*
1347 * Compute the next wakeup time for a given wakeup reason. Can be called to
1348 * initialize a wakeup time, to adjust it for the next wakeup, or to
1349 * reinitialize it when GUCs have changed. We ask the caller to pass in the
1350 * value of "now" because this frequently avoids multiple calls of
1351 * GetCurrentTimestamp(). It had better be a reasonably up-to-date value
1352 * though.
1353 */
1354static void
1356{
1357 switch (reason)
1358 {
1360 if (wal_receiver_timeout <= 0)
1361 wakeup[reason] = TIMESTAMP_INFINITY;
1362 else
1364 break;
1365 case WALRCV_WAKEUP_PING:
1366 if (wal_receiver_timeout <= 0)
1367 wakeup[reason] = TIMESTAMP_INFINITY;
1368 else
1370 break;
1373 wakeup[reason] = TIMESTAMP_INFINITY;
1374 else
1376 break;
1379 wakeup[reason] = TIMESTAMP_INFINITY;
1380 else
1382 break;
1383 /* there's intentionally no default: here */
1384 }
1385}
1386
1387/*
1388 * Wake up the walreceiver main loop.
1389 *
1390 * This is called by the startup process whenever interesting xlog records
1391 * are applied, so that walreceiver can check if it needs to send an apply
1392 * notification back to the primary which may be waiting in a COMMIT with
1393 * synchronous_commit = remote_apply.
1394 */
1395void
1397{
1398 ProcNumber procno;
1399
1401 /* fetching the proc number is probably atomic, but don't rely on it */
1403 procno = WalRcv->procno;
1405 if (procno != INVALID_PROC_NUMBER)
1406 SetLatch(&GetPGProcByNumber(procno)->procLatch);
1407}
1408
1409/*
1410 * Return a string constant representing the state. This is used
1411 * in system functions and views, and should *not* be translated.
1412 */
1413static const char *
1415{
1416 switch (state)
1417 {
1418 case WALRCV_STOPPED:
1419 return "stopped";
1420 case WALRCV_STARTING:
1421 return "starting";
1422 case WALRCV_CONNECTING:
1423 return "connecting";
1424 case WALRCV_STREAMING:
1425 return "streaming";
1426 case WALRCV_WAITING:
1427 return "waiting";
1428 case WALRCV_RESTARTING:
1429 return "restarting";
1430 case WALRCV_STOPPING:
1431 return "stopping";
1432 }
1433 return "UNKNOWN";
1434}
1435
1436/*
1437 * Returns activity of WAL receiver, including pid, state and xlog locations
1438 * received from the WAL sender of another server.
1439 */
1440Datum
1442{
1443 TupleDesc tupdesc;
1444 Datum *values;
1445 bool *nulls;
1446 int pid;
1447 bool ready_to_display;
1454 TimestampTz last_send_time;
1458 char sender_host[NI_MAXHOST];
1459 int sender_port = 0;
1460 char slotname[NAMEDATALEN];
1461 char conninfo[MAXCONNINFO];
1462
1463 /* Take a lock to ensure value consistency */
1465 pid = (int) WalRcv->pid;
1466 ready_to_display = WalRcv->ready_to_display;
1472 last_send_time = WalRcv->lastMsgSendTime;
1476 strlcpy(slotname, WalRcv->slotname, sizeof(slotname));
1477 strlcpy(sender_host, WalRcv->sender_host, sizeof(sender_host));
1478 sender_port = WalRcv->sender_port;
1479 strlcpy(conninfo, WalRcv->conninfo, sizeof(conninfo));
1481
1482 /*
1483 * No WAL receiver (or not ready yet), just return a tuple with NULL
1484 * values
1485 */
1486 if (pid == 0 || !ready_to_display)
1488
1489 /*
1490 * Read "writtenUpto" without holding a spinlock. Note that it may not be
1491 * consistent with the other shared variables of the WAL receiver
1492 * protected by a spinlock, but this should not be used for data integrity
1493 * checks.
1494 */
1496
1497 /* determine result type */
1498 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1499 elog(ERROR, "return type must be a row type");
1500
1501 values = palloc0_array(Datum, tupdesc->natts);
1502 nulls = palloc0_array(bool, tupdesc->natts);
1503
1504 /* Fetch values */
1505 values[0] = Int32GetDatum(pid);
1506
1508 {
1509 /*
1510 * Only superusers and roles with privileges of pg_read_all_stats can
1511 * see details. Other users only get the pid value to know whether it
1512 * is a WAL receiver, but no details.
1513 */
1514 memset(&nulls[1], true, sizeof(bool) * (tupdesc->natts - 1));
1515 }
1516 else
1517 {
1519
1521 nulls[2] = true;
1522 else
1526 nulls[4] = true;
1527 else
1530 nulls[5] = true;
1531 else
1534 if (last_send_time == 0)
1535 nulls[7] = true;
1536 else
1537 values[7] = TimestampTzGetDatum(last_send_time);
1538 if (last_receipt_time == 0)
1539 nulls[8] = true;
1540 else
1543 nulls[9] = true;
1544 else
1546 if (latest_end_time == 0)
1547 nulls[10] = true;
1548 else
1550 if (*slotname == '\0')
1551 nulls[11] = true;
1552 else
1553 values[11] = CStringGetTextDatum(slotname);
1554 if (*sender_host == '\0')
1555 nulls[12] = true;
1556 else
1557 values[12] = CStringGetTextDatum(sender_host);
1558 if (sender_port == 0)
1559 nulls[13] = true;
1560 else
1561 values[13] = Int32GetDatum(sender_port);
1562 if (*conninfo == '\0')
1563 nulls[14] = true;
1564 else
1565 values[14] = CStringGetTextDatum(conninfo);
1566 }
1567
1568 /* Returns the record as Datum */
1570}
bool has_privs_of_role(Oid member, Oid role)
Definition acl.c:5314
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
#define pg_memory_barrier()
Definition atomics.h:141
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:41
void writeTimeLineHistoryFile(TimeLineID tli, char *content, int size)
Definition timeline.c:464
bool existsTimeLineHistory(TimeLineID probeTLI)
Definition timeline.c:223
sigset_t UnBlockSig
Definition pqsignal.c:22
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1751
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1639
const char * timestamptz_to_str(TimestampTz t)
Definition timestamp.c:1856
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1603
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define Min(x, y)
Definition c.h:1091
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
#define UINT64_FORMAT
Definition c.h:635
uint32_t uint32
Definition c.h:624
#define pg_fallthrough
Definition c.h:161
uint32 TransactionId
Definition c.h:736
size_t Size
Definition c.h:689
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:149
Datum arg
Definition elog.c:1323
int errcode_for_file_access(void)
Definition elog.c:898
bool message_level_is_interesting(int elevel)
Definition elog.c:285
int errcode(int sqlerrcode)
Definition elog.c:875
#define LOG
Definition elog.h:32
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:42
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:30
#define PANIC
Definition elog.h:44
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
void err(int eval, const char *fmt,...)
Definition err.c:43
#define ERRCODE_PROTOCOL_VIOLATION
Definition fe-connect.c:96
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#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:49
ProcNumber MyProcNumber
Definition globals.c:92
struct Latch * MyLatch
Definition globals.c:65
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
char * cluster_name
Definition guc_tables.c:582
return str start
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
#define close(a)
Definition win32.h:12
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:372
void proc_exit(int code)
Definition ipc.c:105
int i
Definition isn.c:77
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition latch.c:223
void SetLatch(Latch *latch)
Definition latch.c:290
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
Oid GetUserId(void)
Definition miscinit.c:470
static char * errmsg
#define NAMEDATALEN
const void size_t len
static Datum LSNGetDatum(XLogRecPtr X)
Definition pg_lsn.h:31
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define die(msg)
@ IOOBJECT_WAL
Definition pgstat.h:283
@ IOCONTEXT_NORMAL
Definition pgstat.h:293
@ IOOP_WRITE
Definition pgstat.h:320
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition pgstat_io.c:91
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:122
void pgstat_report_wal(bool force)
Definition pgstat_wal.c:46
#define pqsignal
Definition port.h:547
#define pg_pwrite
Definition port.h:248
#define PG_SIG_IGN
Definition port.h:551
int pgsocket
Definition port.h:29
#define snprintf
Definition port.h:260
#define PGINVALID_SOCKET
Definition port.h:31
#define PG_SIG_DFL
Definition port.h:550
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
off_t pgoff_t
Definition port.h:421
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define PointerGetDatum(X)
Definition postgres.h:354
int pq_getmsgbyte(StringInfo msg)
Definition pqformat.c:398
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
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
static int fb(int x)
#define GetPGProcByNumber(n)
Definition proc.h:504
void GetReplicationHorizons(TransactionId *xmin, TransactionId *catalog_xmin)
Definition procarray.c:1986
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int ProcNumber
Definition procnumber.h:24
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:688
#define PqReplMsg_WALData
Definition protocol.h:77
#define PqReplMsg_Keepalive
Definition protocol.h:75
#define PqReplMsg_HotStandbyFeedback
Definition protocol.h:82
#define PqReplMsg_StandbyStatusUpdate
Definition protocol.h:84
bool update_process_title
Definition ps_status.c:31
static void set_ps_display(const char *activity)
Definition ps_status.h:40
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
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
pg_atomic_uint64 minWaitedLSN[WAIT_LSN_TYPE_COUNT]
Definition xlogwait.h:85
TimestampTz lastMsgReceiptTime
XLogRecPtr latestWalEnd
TimeLineID receiveStartTLI
Definition walreceiver.h:88
TimeLineID receivedTLI
Definition walreceiver.h:98
char slotname[NAMEDATALEN]
char sender_host[NI_MAXHOST]
XLogRecPtr receiveStart
Definition walreceiver.h:87
XLogRecPtr flushedUpto
Definition walreceiver.h:97
ProcNumber procno
Definition walreceiver.h:68
pg_atomic_uint64 writtenUpto
TimestampTz lastMsgSendTime
WalRcvState walRcvState
Definition walreceiver.h:72
TimestampTz latestWalEndTime
sig_atomic_t apply_reply_requested
bool ready_to_display
slock_t mutex
char conninfo[MAXCONNINFO]
#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:283
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:67
static void pgstat_report_wait_end(void)
Definition wait_event.h:83
const char * type
#define WL_SOCKET_READABLE
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define NUM_WALRCV_WAKEUPS
static WalReceiverConn * wrconn
Definition walreceiver.c:95
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]
void WalReceiverMain(const void *startup_data, size_t startup_data_len)
static StringInfoData reply_message
bool hot_standby_feedback
Definition walreceiver.c:92
XLogRecPtr Flush
static int recvFile
static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
int wal_receiver_status_interval
Definition walreceiver.c:90
static void WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last)
XLogRecPtr Write
static void XLogWalRcvFlush(bool dying, TimeLineID tli)
static TimeLineID recvFileTLI
WalReceiverFunctionsType * WalReceiverFunctions
Definition walreceiver.c:96
static void XLogWalRcvSendReply(bool force, bool requestReply, bool checkApply)
static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
Datum pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
int wal_receiver_timeout
Definition walreceiver.c:91
static XLogSegNo recvSegNo
static void XLogWalRcvClose(XLogRecPtr recptr, TimeLineID tli)
static void XLogWalRcvSendHSFeedback(bool immed)
WalRcvWakeupReason
@ WALRCV_WAKEUP_TERMINATE
@ WALRCV_WAKEUP_REPLY
@ WALRCV_WAKEUP_PING
@ WALRCV_WAKEUP_HSFEEDBACK
static void WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
static void WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now)
static void WalRcvDie(int code, Datum arg)
void WalRcvRequestApplyReply(void)
static struct @19 LogstreamResult
static const char * WalRcvGetStateString(WalRcvState state)
#define AllowCascadeReplication()
Definition walreceiver.h:40
#define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
#define walrcv_startstreaming(conn, options)
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
#define walrcv_send(conn, buffer, nbytes)
#define walrcv_get_senderinfo(conn, sender_host, sender_port)
#define MAXCONNINFO
Definition walreceiver.h:37
#define walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)
#define walrcv_get_conninfo(conn)
#define walrcv_endstreaming(conn, next_tli)
WalRcvState
Definition walreceiver.h:46
@ WALRCV_STARTING
Definition walreceiver.h:48
@ WALRCV_STOPPED
Definition walreceiver.h:47
@ WALRCV_CONNECTING
Definition walreceiver.h:50
@ WALRCV_RESTARTING
Definition walreceiver.h:53
@ WALRCV_STREAMING
Definition walreceiver.h:51
@ WALRCV_WAITING
Definition walreceiver.h:52
@ WALRCV_STOPPING
Definition walreceiver.h:54
#define walrcv_identify_system(conn, primary_tli)
#define walrcv_disconnect(conn)
#define walrcv_get_backend_pid(conn)
#define walrcv_receive(conn, buffer, wait_fd)
WalRcvData * WalRcv
int GetReplicationApplyDelay(void)
int GetReplicationTransferLatency(void)
void WalSndWakeup(bool physical, bool logical)
Definition walsender.c:4012
#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:3435
uint64 GetSystemIdentifier(void)
Definition xlog.c:4647
bool RecoveryInProgress(void)
Definition xlog.c:6836
int XLogArchiveMode
Definition xlog.c:126
int wal_segment_size
Definition xlog.c:150
bool track_wal_io_timing
Definition xlog.c:144
void issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
Definition xlog.c:9362
@ ARCHIVE_MODE_ALWAYS
Definition xlog.h:69
#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)
void XLogArchiveNotify(const char *xlog)
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
uint32 TimeLineID
Definition xlogdefs.h:63
uint64 XLogSegNo
Definition xlogdefs.h:52
bool HotStandbyActive(void)
void WakeupRecovery(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
struct WaitLSNState * waitLSNState
Definition xlogwait.c:70
void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN)
Definition xlogwait.c:320
@ WAIT_LSN_TYPE_STANDBY_FLUSH
Definition xlogwait.h:41
@ WAIT_LSN_TYPE_STANDBY_WRITE
Definition xlogwait.h:40