1 /*-------------------------------------------------------------------------
5 * The WAL sender process (walsender) is new as of Postgres 9.0. It takes
6 * care of sending XLOG from the primary server to a single recipient.
7 * (Note that there can be more than one walsender process concurrently.)
8 * It is started by the postmaster when the walreceiver of a standby server
9 * connects to the primary server and requests XLOG streaming replication.
10 * It attempts to keep reading XLOG records from the disk and sending them
11 * to the standby server, as long as the connection is alive (i.e., like
12 * any backend, there is a one-to-one relationship between a connection
13 * and a walsender process).
15 * Normal termination is by SIGTERM, which instructs the walsender to
16 * close the connection and exit(0) at next convenient moment. Emergency
17 * termination is by SIGQUIT; like any backend, the walsender will simply
18 * abort and exit on SIGQUIT. A close of the connection and a FATAL error
19 * are treated as not a crash but approximately normal termination;
20 * the walsender will exit quickly without sending any more XLOG records.
22 * If the server is shut down, postmaster sends us SIGUSR2 after all
23 * regular backends have exited and the shutdown checkpoint has been written.
24 * This instruct walsender to send any outstanding WAL, including the
25 * shutdown checkpoint record, and then exit.
28 * Portions Copyright (c) 2010-2012, PostgreSQL Global Development Group
31 * src/backend/replication/walsender.c
33 *-------------------------------------------------------------------------
40 #include "access/transam.h"
41 #include "access/xlog_internal.h"
42 #include "catalog/pg_type.h"
44 #include "libpq/libpq.h"
45 #include "libpq/pqformat.h"
46 #include "libpq/pqsignal.h"
47 #include "miscadmin.h"
48 #include "nodes/replnodes.h"
49 #include "replication/basebackup.h"
50 #include "replication/syncrep.h"
51 #include "replication/walprotocol.h"
52 #include "replication/walreceiver.h"
53 #include "replication/walsender.h"
54 #include "replication/walsender_private.h"
55 #include "storage/fd.h"
56 #include "storage/ipc.h"
57 #include "storage/pmsignal.h"
58 #include "storage/proc.h"
59 #include "storage/procarray.h"
60 #include "tcop/tcopprot.h"
61 #include "utils/builtins.h"
62 #include "utils/guc.h"
63 #include "utils/memutils.h"
64 #include "utils/ps_status.h"
65 #include "utils/resowner.h"
66 #include "utils/timestamp.h"
69 /* Array of WalSnds in shared memory */
70 WalSndCtlData *WalSndCtl = NULL;
72 /* My slot in the shared memory array */
73 WalSnd *MyWalSnd = NULL;
76 bool am_walsender = false; /* Am I a walsender process ? */
77 bool am_cascading_walsender = false; /* Am I cascading WAL to
78 * another standby ? */
80 /* User-settable parameters for walsender */
81 int max_wal_senders = 0; /* the maximum number of concurrent walsenders */
82 int replication_timeout = 60 * 1000; /* maximum time to send one
85 * State for WalSndWakeupRequest
87 bool wake_wal_senders = false;
90 * These variables are used similarly to openLogFile/Id/Seg/Off,
91 * but for walsender to read the XLOG.
93 static int sendFile = -1;
94 static XLogSegNo sendSegNo = 0;
95 static uint32 sendOff = 0;
98 * How far have we sent WAL already? This is also advertised in
99 * MyWalSnd->sentPtr. (Actually, this is the next WAL location to send.)
101 static XLogRecPtr sentPtr = 0;
104 * Buffer for processing reply messages.
106 static StringInfoData reply_message;
109 * Timestamp of the last receipt of the reply from the standby.
111 static TimestampTz last_reply_timestamp;
113 /* Flags set by signal handlers for later service in main loop */
114 static volatile sig_atomic_t got_SIGHUP = false;
115 volatile sig_atomic_t walsender_shutdown_requested = false;
116 volatile sig_atomic_t walsender_ready_to_stop = false;
118 /* Signal handlers */
119 static void WalSndSigHupHandler(SIGNAL_ARGS);
120 static void WalSndShutdownHandler(SIGNAL_ARGS);
121 static void WalSndQuickDieHandler(SIGNAL_ARGS);
122 static void WalSndXLogSendHandler(SIGNAL_ARGS);
123 static void WalSndLastCycleHandler(SIGNAL_ARGS);
125 /* Prototypes for private functions */
126 static bool HandleReplicationCommand(const char *cmd_string);
127 static void WalSndLoop(void) __attribute__((noreturn));
128 static void InitWalSnd(void);
129 static void WalSndHandshake(void);
130 static void WalSndKill(int code, Datum arg);
131 static void XLogSend(char *msgbuf, bool *caughtup);
132 static void IdentifySystem(void);
133 static void StartReplication(StartReplicationCmd *cmd);
134 static void ProcessStandbyMessage(void);
135 static void ProcessStandbyReplyMessage(void);
136 static void ProcessStandbyHSFeedbackMessage(void);
137 static void ProcessRepliesIfAny(void);
138 static void WalSndKeepalive(char *msgbuf);
141 /* Main entry point for walsender process */
145 MemoryContext walsnd_context;
147 am_cascading_walsender = RecoveryInProgress();
149 /* Create a per-walsender data structure in shared memory */
153 * Create a memory context that we will do all our work in. We do this so
154 * that we can reset the context during error recovery and thereby avoid
155 * possible memory leaks. Formerly this code just ran in
156 * TopMemoryContext, but resetting that would be a really bad idea.
158 * XXX: we don't actually attempt error recovery in walsender, we just
159 * close the connection and exit.
161 walsnd_context = AllocSetContextCreate(TopMemoryContext,
163 ALLOCSET_DEFAULT_MINSIZE,
164 ALLOCSET_DEFAULT_INITSIZE,
165 ALLOCSET_DEFAULT_MAXSIZE);
166 MemoryContextSwitchTo(walsnd_context);
168 /* Set up resource owner */
169 CurrentResourceOwner = ResourceOwnerCreate(NULL, "walsender top-level resource owner");
171 /* Unblock signals (they were blocked when the postmaster forked us) */
172 PG_SETMASK(&UnBlockSig);
175 * Use the recovery target timeline ID during recovery
177 if (am_cascading_walsender)
178 ThisTimeLineID = GetRecoveryTargetTLI();
180 /* Tell the standby that walsender is ready for receiving commands */
181 ReadyForQuery(DestRemote);
183 /* Handle handshake messages before streaming */
186 /* Initialize shared memory status */
188 /* use volatile pointer to prevent code rearrangement */
189 volatile WalSnd *walsnd = MyWalSnd;
191 SpinLockAcquire(&walsnd->mutex);
192 walsnd->sentPtr = sentPtr;
193 SpinLockRelease(&walsnd->mutex);
198 /* Main loop of walsender */
203 * Execute commands from walreceiver, until we enter streaming mode.
206 WalSndHandshake(void)
208 StringInfoData input_message;
209 bool replication_started = false;
211 initStringInfo(&input_message);
213 while (!replication_started)
217 WalSndSetState(WALSNDSTATE_STARTUP);
218 set_ps_display("idle", false);
220 /* Wait for a command to arrive */
221 firstchar = pq_getbyte();
224 * Emergency bailout if postmaster has died. This is to avoid the
225 * necessity for manual cleanup of all postmaster children.
227 if (!PostmasterIsAlive())
231 * Check for any other interesting events that happened while we
237 ProcessConfigFile(PGC_SIGHUP);
240 if (firstchar != EOF)
243 * Read the message contents. This is expected to be done without
244 * blocking because we've been able to get message type code.
246 if (pq_getmessage(&input_message, 0))
247 firstchar = EOF; /* suitable message already logged */
250 /* Handle the very limited subset of commands expected in this phase */
253 case 'Q': /* Query message */
255 const char *query_string;
257 query_string = pq_getmsgstring(&input_message);
258 pq_getmsgend(&input_message);
260 if (HandleReplicationCommand(query_string))
261 replication_started = true;
266 /* standby is closing the connection */
270 /* standby disconnected unexpectedly */
272 (errcode(ERRCODE_PROTOCOL_VIOLATION),
273 errmsg("unexpected EOF on standby connection")));
278 (errcode(ERRCODE_PROTOCOL_VIOLATION),
279 errmsg("invalid standby handshake message type %d", firstchar)));
293 char xpos[MAXFNAMELEN];
297 * Reply with a result set with one row, three columns. First col is
298 * system ID, second is timeline ID, and third is current xlog location.
301 snprintf(sysid, sizeof(sysid), UINT64_FORMAT,
302 GetSystemIdentifier());
303 snprintf(tli, sizeof(tli), "%u", ThisTimeLineID);
305 logptr = am_cascading_walsender ? GetStandbyFlushRecPtr() : GetInsertRecPtr();
307 snprintf(xpos, sizeof(xpos), "%X/%X", (uint32) (logptr >> 32), (uint32) logptr);
309 /* Send a RowDescription message */
310 pq_beginmessage(&buf, 'T');
311 pq_sendint(&buf, 3, 2); /* 3 fields */
314 pq_sendstring(&buf, "systemid"); /* col name */
315 pq_sendint(&buf, 0, 4); /* table oid */
316 pq_sendint(&buf, 0, 2); /* attnum */
317 pq_sendint(&buf, TEXTOID, 4); /* type oid */
318 pq_sendint(&buf, -1, 2); /* typlen */
319 pq_sendint(&buf, 0, 4); /* typmod */
320 pq_sendint(&buf, 0, 2); /* format code */
323 pq_sendstring(&buf, "timeline"); /* col name */
324 pq_sendint(&buf, 0, 4); /* table oid */
325 pq_sendint(&buf, 0, 2); /* attnum */
326 pq_sendint(&buf, INT4OID, 4); /* type oid */
327 pq_sendint(&buf, 4, 2); /* typlen */
328 pq_sendint(&buf, 0, 4); /* typmod */
329 pq_sendint(&buf, 0, 2); /* format code */
332 pq_sendstring(&buf, "xlogpos");
333 pq_sendint(&buf, 0, 4);
334 pq_sendint(&buf, 0, 2);
335 pq_sendint(&buf, TEXTOID, 4);
336 pq_sendint(&buf, -1, 2);
337 pq_sendint(&buf, 0, 4);
338 pq_sendint(&buf, 0, 2);
341 /* Send a DataRow message */
342 pq_beginmessage(&buf, 'D');
343 pq_sendint(&buf, 3, 2); /* # of columns */
344 pq_sendint(&buf, strlen(sysid), 4); /* col1 len */
345 pq_sendbytes(&buf, (char *) &sysid, strlen(sysid));
346 pq_sendint(&buf, strlen(tli), 4); /* col2 len */
347 pq_sendbytes(&buf, (char *) tli, strlen(tli));
348 pq_sendint(&buf, strlen(xpos), 4); /* col3 len */
349 pq_sendbytes(&buf, (char *) xpos, strlen(xpos));
353 /* Send CommandComplete and ReadyForQuery messages */
354 EndCommand("SELECT", DestRemote);
355 ReadyForQuery(DestRemote);
356 /* ReadyForQuery did pq_flush for us */
363 StartReplication(StartReplicationCmd *cmd)
368 * Let postmaster know that we're streaming. Once we've declared us as a
369 * WAL sender process, postmaster will let us outlive the bgwriter and
370 * kill us last in the shutdown sequence, so we get a chance to stream all
371 * remaining WAL at shutdown, including the shutdown checkpoint. Note that
372 * there's no going back, and we mustn't write any WAL records after this.
374 MarkPostmasterChildWalSender();
375 SendPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE);
378 * When promoting a cascading standby, postmaster sends SIGUSR2 to any
379 * cascading walsenders to kill them. But there is a corner-case where
380 * such walsender fails to receive SIGUSR2 and survives a standby
381 * promotion unexpectedly. This happens when postmaster sends SIGUSR2
382 * before the walsender marks itself as a WAL sender, because postmaster
383 * sends SIGUSR2 to only the processes marked as a WAL sender.
385 * To avoid this corner-case, if recovery is NOT in progress even though
386 * the walsender is cascading one, we do the same thing as SIGUSR2 signal
387 * handler does, i.e., set walsender_ready_to_stop to true. Which causes
388 * the walsender to end later.
390 * When terminating cascading walsenders, usually postmaster writes the
391 * log message announcing the terminations. But there is a race condition
392 * here. If there is no walsender except this process before reaching
393 * here, postmaster thinks that there is no walsender and suppresses that
394 * log message. To handle this case, we always emit that log message here.
395 * This might cause duplicate log messages, but which is less likely to
396 * happen, so it's not worth writing some code to suppress them.
398 if (am_cascading_walsender && !RecoveryInProgress())
401 (errmsg("terminating walsender process to force cascaded standby "
402 "to update timeline and reconnect")));
403 walsender_ready_to_stop = true;
407 * We assume here that we're logging enough information in the WAL for
408 * log-shipping, since this is checked in PostmasterMain().
410 * NOTE: wal_level can only change at shutdown, so in most cases it is
411 * difficult for there to be WAL data that we can still see that was
412 * written at wal_level='minimal'.
416 * When we first start replication the standby will be behind the primary.
417 * For some applications, for example, synchronous replication, it is
418 * important to have a clear state for this initial catchup mode, so we
419 * can trigger actions when we change streaming state later. We may stay
420 * in this state for a long time, which is exactly why we want to be able
421 * to monitor whether or not we are still here.
423 WalSndSetState(WALSNDSTATE_CATCHUP);
425 /* Send a CopyBothResponse message, and start streaming */
426 pq_beginmessage(&buf, 'W');
427 pq_sendbyte(&buf, 0);
428 pq_sendint(&buf, 0, 2);
433 * Initialize position to the received one, then the xlog records begin to
434 * be shipped from that position
436 sentPtr = cmd->startpoint;
440 * Execute an incoming replication command.
443 HandleReplicationCommand(const char *cmd_string)
445 bool replication_started = false;
448 MemoryContext cmd_context;
449 MemoryContext old_context;
451 elog(DEBUG1, "received replication command: %s", cmd_string);
453 cmd_context = AllocSetContextCreate(CurrentMemoryContext,
454 "Replication command context",
455 ALLOCSET_DEFAULT_MINSIZE,
456 ALLOCSET_DEFAULT_INITSIZE,
457 ALLOCSET_DEFAULT_MAXSIZE);
458 old_context = MemoryContextSwitchTo(cmd_context);
460 replication_scanner_init(cmd_string);
461 parse_rc = replication_yyparse();
464 (errcode(ERRCODE_SYNTAX_ERROR),
465 (errmsg_internal("replication command parser returned %d",
468 cmd_node = replication_parse_result;
470 switch (cmd_node->type)
472 case T_IdentifySystemCmd:
476 case T_StartReplicationCmd:
477 StartReplication((StartReplicationCmd *) cmd_node);
479 /* break out of the loop */
480 replication_started = true;
483 case T_BaseBackupCmd:
484 SendBaseBackup((BaseBackupCmd *) cmd_node);
486 /* Send CommandComplete and ReadyForQuery messages */
487 EndCommand("SELECT", DestRemote);
488 ReadyForQuery(DestRemote);
489 /* ReadyForQuery did pq_flush for us */
494 (errcode(ERRCODE_PROTOCOL_VIOLATION),
495 errmsg("invalid standby query string: %s", cmd_string)));
499 MemoryContextSwitchTo(old_context);
500 MemoryContextDelete(cmd_context);
502 return replication_started;
506 * Check if the remote end has closed the connection.
509 ProcessRepliesIfAny(void)
511 unsigned char firstchar;
513 bool received = false;
517 r = pq_getbyte_if_available(&firstchar);
520 /* unexpected error or EOF */
522 (errcode(ERRCODE_PROTOCOL_VIOLATION),
523 errmsg("unexpected EOF on standby connection")));
528 /* no data available without blocking */
532 /* Handle the very limited subset of commands expected in this phase */
536 * 'd' means a standby reply wrapped in a CopyData packet.
539 ProcessStandbyMessage();
544 * 'X' means that the standby is closing down the socket.
551 (errcode(ERRCODE_PROTOCOL_VIOLATION),
552 errmsg("invalid standby message type \"%c\"",
558 * Save the last reply timestamp if we've received at least one reply.
561 last_reply_timestamp = GetCurrentTimestamp();
565 * Process a status update message received from standby.
568 ProcessStandbyMessage(void)
572 resetStringInfo(&reply_message);
575 * Read the message contents.
577 if (pq_getmessage(&reply_message, 0))
580 (errcode(ERRCODE_PROTOCOL_VIOLATION),
581 errmsg("unexpected EOF on standby connection")));
586 * Check message type from the first byte.
588 msgtype = pq_getmsgbyte(&reply_message);
593 ProcessStandbyReplyMessage();
597 ProcessStandbyHSFeedbackMessage();
602 (errcode(ERRCODE_PROTOCOL_VIOLATION),
603 errmsg("unexpected message type \"%c\"", msgtype)));
609 * Regular reply from standby advising of WAL positions on standby server.
612 ProcessStandbyReplyMessage(void)
614 StandbyReplyMessage reply;
616 pq_copymsgbytes(&reply_message, (char *) &reply, sizeof(StandbyReplyMessage));
618 elog(DEBUG2, "write %X/%X flush %X/%X apply %X/%X",
619 (uint32) (reply.write >> 32), (uint32) reply.write,
620 (uint32) (reply.flush >> 32), (uint32) reply.flush,
621 (uint32) (reply.apply >> 32), (uint32) reply.apply);
624 * Update shared state for this WalSender process based on reply data from
628 /* use volatile pointer to prevent code rearrangement */
629 volatile WalSnd *walsnd = MyWalSnd;
631 SpinLockAcquire(&walsnd->mutex);
632 walsnd->write = reply.write;
633 walsnd->flush = reply.flush;
634 walsnd->apply = reply.apply;
635 SpinLockRelease(&walsnd->mutex);
638 if (!am_cascading_walsender)
639 SyncRepReleaseWaiters();
643 * Hot Standby feedback
646 ProcessStandbyHSFeedbackMessage(void)
648 StandbyHSFeedbackMessage reply;
649 TransactionId nextXid;
652 /* Decipher the reply message */
653 pq_copymsgbytes(&reply_message, (char *) &reply,
654 sizeof(StandbyHSFeedbackMessage));
656 elog(DEBUG2, "hot standby feedback xmin %u epoch %u",
660 /* Ignore invalid xmin (can't actually happen with current walreceiver) */
661 if (!TransactionIdIsNormal(reply.xmin))
665 * Check that the provided xmin/epoch are sane, that is, not in the future
666 * and not so far back as to be already wrapped around. Ignore if not.
668 * Epoch of nextXid should be same as standby, or if the counter has
669 * wrapped, then one greater than standby.
671 GetNextXidAndEpoch(&nextXid, &nextEpoch);
673 if (reply.xmin <= nextXid)
675 if (reply.epoch != nextEpoch)
680 if (reply.epoch + 1 != nextEpoch)
684 if (!TransactionIdPrecedesOrEquals(reply.xmin, nextXid))
685 return; /* epoch OK, but it's wrapped around */
688 * Set the WalSender's xmin equal to the standby's requested xmin, so that
689 * the xmin will be taken into account by GetOldestXmin. This will hold
690 * back the removal of dead rows and thereby prevent the generation of
691 * cleanup conflicts on the standby server.
693 * There is a small window for a race condition here: although we just
694 * checked that reply.xmin precedes nextXid, the nextXid could have gotten
695 * advanced between our fetching it and applying the xmin below, perhaps
696 * far enough to make reply.xmin wrap around. In that case the xmin we
697 * set here would be "in the future" and have no effect. No point in
698 * worrying about this since it's too late to save the desired data
699 * anyway. Assuming that the standby sends us an increasing sequence of
700 * xmins, this could only happen during the first reply cycle, else our
701 * own xmin would prevent nextXid from advancing so far.
703 * We don't bother taking the ProcArrayLock here. Setting the xmin field
704 * is assumed atomic, and there's no real need to prevent a concurrent
705 * GetOldestXmin. (If we're moving our xmin forward, this is obviously
706 * safe, and if we're moving it backwards, well, the data is at risk
707 * already since a VACUUM could have just finished calling GetOldestXmin.)
709 MyPgXact->xmin = reply.xmin;
712 /* Main loop of walsender process */
716 char *output_message;
717 bool caughtup = false;
720 * Allocate buffer that will be used for each output message. We do this
721 * just once to reduce palloc overhead. The buffer must be made large
722 * enough for maximum-sized messages.
724 output_message = palloc(1 + sizeof(WalDataMessageHeader) + MAX_SEND_SIZE);
727 * Allocate buffer that will be used for processing reply messages. As
728 * above, do this just once to reduce palloc overhead.
730 initStringInfo(&reply_message);
732 /* Initialize the last reply timestamp */
733 last_reply_timestamp = GetCurrentTimestamp();
735 /* Loop forever, unless we get an error */
738 /* Clear any already-pending wakeups */
739 ResetLatch(&MyWalSnd->latch);
742 * Emergency bailout if postmaster has died. This is to avoid the
743 * necessity for manual cleanup of all postmaster children.
745 if (!PostmasterIsAlive())
748 /* Process any requests or signals received recently */
752 ProcessConfigFile(PGC_SIGHUP);
756 /* Normal exit from the walsender is here */
757 if (walsender_shutdown_requested)
759 /* Inform the standby that XLOG streaming is done */
760 pq_puttextmessage('C', "COPY 0");
766 /* Check for input from the client */
767 ProcessRepliesIfAny();
770 * If we don't have any pending data in the output buffer, try to send
771 * some more. If there is some, we don't bother to call XLogSend
772 * again until we've flushed it ... but we'd better assume we are not
775 if (!pq_is_send_pending())
776 XLogSend(output_message, &caughtup);
780 /* Try to flush pending output to the client */
781 if (pq_flush_if_writable() != 0)
784 /* If nothing remains to be sent right now ... */
785 if (caughtup && !pq_is_send_pending())
788 * If we're in catchup state, move to streaming. This is an
789 * important state change for users to know about, since before
790 * this point data loss might occur if the primary dies and we
791 * need to failover to the standby. The state change is also
792 * important for synchronous replication, since commits that
793 * started to wait at that point might wait for some time.
795 if (MyWalSnd->state == WALSNDSTATE_CATCHUP)
798 (errmsg("standby \"%s\" has now caught up with primary",
800 WalSndSetState(WALSNDSTATE_STREAMING);
804 * When SIGUSR2 arrives, we send any outstanding logs up to the
805 * shutdown checkpoint record (i.e., the latest record) and exit.
806 * This may be a normal termination at shutdown, or a promotion,
807 * the walsender is not sure which.
809 if (walsender_ready_to_stop)
811 /* ... let's just be real sure we're caught up ... */
812 XLogSend(output_message, &caughtup);
813 if (caughtup && !pq_is_send_pending())
815 walsender_shutdown_requested = true;
816 continue; /* don't want to wait more */
822 * We don't block if not caught up, unless there is unsent data
823 * pending in which case we'd better block until the socket is
824 * write-ready. This test is only needed for the case where XLogSend
825 * loaded a subset of the available data but then pq_flush_if_writable
826 * flushed it all --- we should immediately try to send more.
828 if (caughtup || pq_is_send_pending())
830 TimestampTz timeout = 0;
831 long sleeptime = 10000; /* 10 s */
834 wakeEvents = WL_LATCH_SET | WL_POSTMASTER_DEATH |
835 WL_SOCKET_READABLE | WL_TIMEOUT;
837 if (pq_is_send_pending())
838 wakeEvents |= WL_SOCKET_WRITEABLE;
841 WalSndKeepalive(output_message);
842 /* Try to flush pending output to the client */
843 if (pq_flush_if_writable() != 0)
847 /* Determine time until replication timeout */
848 if (replication_timeout > 0)
850 timeout = TimestampTzPlusMilliseconds(last_reply_timestamp,
851 replication_timeout);
852 sleeptime = 1 + (replication_timeout / 10);
855 /* Sleep until something happens or replication timeout */
856 WaitLatchOrSocket(&MyWalSnd->latch, wakeEvents,
857 MyProcPort->sock, sleeptime);
860 * Check for replication timeout. Note we ignore the corner case
861 * possibility that the client replied just as we reached the
862 * timeout ... he's supposed to reply *before* that.
864 if (replication_timeout > 0 &&
865 GetCurrentTimestamp() >= timeout)
868 * Since typically expiration of replication timeout means
869 * communication problem, we don't send the error message to
873 (errmsg("terminating walsender process due to replication timeout")));
880 * Get here on send failure. Clean up and exit.
882 * Reset whereToSendOutput to prevent ereport from attempting to send any
883 * more messages to the standby.
885 if (whereToSendOutput == DestRemote)
886 whereToSendOutput = DestNone;
889 abort(); /* keep the compiler quiet */
892 /* Initialize a per-walsender data structure for this walsender process */
899 * WalSndCtl should be set up already (we inherit this by fork() or
900 * EXEC_BACKEND mechanism from the postmaster).
902 Assert(WalSndCtl != NULL);
903 Assert(MyWalSnd == NULL);
906 * Find a free walsender slot and reserve it. If this fails, we must be
907 * out of WalSnd structures.
909 for (i = 0; i < max_wal_senders; i++)
911 /* use volatile pointer to prevent code rearrangement */
912 volatile WalSnd *walsnd = &WalSndCtl->walsnds[i];
914 SpinLockAcquire(&walsnd->mutex);
916 if (walsnd->pid != 0)
918 SpinLockRelease(&walsnd->mutex);
924 * Found a free slot. Reserve it for us.
926 walsnd->pid = MyProcPid;
927 MemSet(&walsnd->sentPtr, 0, sizeof(XLogRecPtr));
928 walsnd->state = WALSNDSTATE_STARTUP;
929 SpinLockRelease(&walsnd->mutex);
930 /* don't need the lock anymore */
931 OwnLatch((Latch *) &walsnd->latch);
932 MyWalSnd = (WalSnd *) walsnd;
937 if (MyWalSnd == NULL)
939 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
940 errmsg("number of requested standby connections "
941 "exceeds max_wal_senders (currently %d)",
944 /* Arrange to clean up at walsender exit */
945 on_shmem_exit(WalSndKill, 0);
948 /* Destroy the per-walsender data structure for this walsender process */
950 WalSndKill(int code, Datum arg)
952 Assert(MyWalSnd != NULL);
955 * Mark WalSnd struct no longer in use. Assume that no lock is required
959 DisownLatch(&MyWalSnd->latch);
961 /* WalSnd struct isn't mine anymore */
966 * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'
968 * XXX probably this should be improved to suck data directly from the
969 * WAL buffers when possible.
971 * Will open, and keep open, one WAL segment stored in the global file
972 * descriptor sendFile. This means if XLogRead is used once, there will
973 * always be one descriptor left open until the process ends, but never
977 XLogRead(char *buf, XLogRecPtr startptr, Size count)
982 XLogSegNo lastRemovedSegNo;
996 startoff = recptr % XLogSegSize;
998 if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
1000 char path[MAXPGPATH];
1002 /* Switch to another logfile segment */
1006 XLByteToSeg(recptr, sendSegNo);
1007 XLogFilePath(path, ThisTimeLineID, sendSegNo);
1009 sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
1013 * If the file is not found, assume it's because the standby
1014 * asked for a too old WAL segment that has already been
1015 * removed or recycled.
1017 if (errno == ENOENT)
1019 (errcode_for_file_access(),
1020 errmsg("requested WAL segment %s has already been removed",
1021 XLogFileNameP(ThisTimeLineID, sendSegNo))));
1024 (errcode_for_file_access(),
1025 errmsg("could not open file \"%s\": %m",
1031 /* Need to seek in the file? */
1032 if (sendOff != startoff)
1034 if (lseek(sendFile, (off_t) startoff, SEEK_SET) < 0)
1036 (errcode_for_file_access(),
1037 errmsg("could not seek in log segment %s to offset %u: %m",
1038 XLogFileNameP(ThisTimeLineID, sendSegNo),
1043 /* How many bytes are within this segment? */
1044 if (nbytes > (XLogSegSize - startoff))
1045 segbytes = XLogSegSize - startoff;
1049 readbytes = read(sendFile, p, segbytes);
1053 (errcode_for_file_access(),
1054 errmsg("could not read from log segment %s, offset %u, length %lu: %m",
1055 XLogFileNameP(ThisTimeLineID, sendSegNo),
1056 sendOff, (unsigned long) segbytes)));
1059 /* Update state for read */
1060 XLByteAdvance(recptr, readbytes);
1062 sendOff += readbytes;
1063 nbytes -= readbytes;
1068 * After reading into the buffer, check that what we read was valid. We do
1069 * this after reading, because even though the segment was present when we
1070 * opened it, it might get recycled or removed while we read it. The
1071 * read() succeeds in that case, but the data we tried to read might
1072 * already have been overwritten with new WAL records.
1074 XLogGetLastRemoved(&lastRemovedSegNo);
1075 XLByteToSeg(startptr, segno);
1076 if (segno <= lastRemovedSegNo)
1078 (errcode_for_file_access(),
1079 errmsg("requested WAL segment %s has already been removed",
1080 XLogFileNameP(ThisTimeLineID, segno))));
1083 * During recovery, the currently-open WAL file might be replaced with the
1084 * file of the same name retrieved from archive. So we always need to
1085 * check what we read was valid after reading into the buffer. If it's
1086 * invalid, we try to open and read the file again.
1088 if (am_cascading_walsender)
1090 /* use volatile pointer to prevent code rearrangement */
1091 volatile WalSnd *walsnd = MyWalSnd;
1094 SpinLockAcquire(&walsnd->mutex);
1095 reload = walsnd->needreload;
1096 walsnd->needreload = false;
1097 SpinLockRelease(&walsnd->mutex);
1099 if (reload && sendFile >= 0)
1110 * Read up to MAX_SEND_SIZE bytes of WAL that's been flushed to disk,
1111 * but not yet sent to the client, and buffer it in the libpq output
1114 * msgbuf is a work area in which the output message is constructed. It's
1115 * passed in just so we can avoid re-palloc'ing the buffer on each cycle.
1116 * It must be of size 1 + sizeof(WalDataMessageHeader) + MAX_SEND_SIZE.
1118 * If there is no unsent WAL remaining, *caughtup is set to true, otherwise
1119 * *caughtup is set to false.
1123 XLogSend(char *msgbuf, bool *caughtup)
1125 XLogRecPtr SendRqstPtr;
1126 XLogRecPtr startptr;
1129 WalDataMessageHeader msghdr;
1132 * Attempt to send all data that's already been written out and fsync'd to
1133 * disk. We cannot go further than what's been written out given the
1134 * current implementation of XLogRead(). And in any case it's unsafe to
1135 * send WAL that is not securely down to disk on the master: if the master
1136 * subsequently crashes and restarts, slaves must not have applied any WAL
1137 * that gets lost on the master.
1139 SendRqstPtr = am_cascading_walsender ? GetStandbyFlushRecPtr() : GetFlushRecPtr();
1141 /* Quick exit if nothing to do */
1142 if (XLByteLE(SendRqstPtr, sentPtr))
1149 * Figure out how much to send in one message. If there's no more than
1150 * MAX_SEND_SIZE bytes to send, send everything. Otherwise send
1151 * MAX_SEND_SIZE bytes, but round back to logfile or page boundary.
1153 * The rounding is not only for performance reasons. Walreceiver relies on
1154 * the fact that we never split a WAL record across two messages. Since a
1155 * long WAL record is split at page boundary into continuation records,
1156 * page boundary is always a safe cut-off point. We also assume that
1157 * SendRqstPtr never points to the middle of a WAL record.
1161 XLByteAdvance(endptr, MAX_SEND_SIZE);
1163 /* if we went beyond SendRqstPtr, back off */
1164 if (XLByteLE(SendRqstPtr, endptr))
1166 endptr = SendRqstPtr;
1171 /* round down to page boundary. */
1172 endptr -= (endptr % XLOG_BLCKSZ);
1176 nbytes = endptr - startptr;
1177 Assert(nbytes <= MAX_SEND_SIZE);
1180 * OK to read and send the slice.
1185 * Read the log directly into the output buffer to avoid extra memcpy
1188 XLogRead(msgbuf + 1 + sizeof(WalDataMessageHeader), startptr, nbytes);
1191 * We fill the message header last so that the send timestamp is taken as
1194 msghdr.dataStart = startptr;
1195 msghdr.walEnd = SendRqstPtr;
1196 msghdr.sendTime = GetCurrentTimestamp();
1198 memcpy(msgbuf + 1, &msghdr, sizeof(WalDataMessageHeader));
1200 pq_putmessage_noblock('d', msgbuf, 1 + sizeof(WalDataMessageHeader) + nbytes);
1204 /* Update shared memory status */
1206 /* use volatile pointer to prevent code rearrangement */
1207 volatile WalSnd *walsnd = MyWalSnd;
1209 SpinLockAcquire(&walsnd->mutex);
1210 walsnd->sentPtr = sentPtr;
1211 SpinLockRelease(&walsnd->mutex);
1214 /* Report progress of XLOG streaming in PS display */
1215 if (update_process_title)
1217 char activitymsg[50];
1219 snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
1220 (uint32) (sentPtr >> 32), (uint32) sentPtr);
1221 set_ps_display(activitymsg, false);
1228 * Request walsenders to reload the currently-open WAL file
1231 WalSndRqstFileReload(void)
1235 for (i = 0; i < max_wal_senders; i++)
1237 /* use volatile pointer to prevent code rearrangement */
1238 volatile WalSnd *walsnd = &WalSndCtl->walsnds[i];
1240 if (walsnd->pid == 0)
1243 SpinLockAcquire(&walsnd->mutex);
1244 walsnd->needreload = true;
1245 SpinLockRelease(&walsnd->mutex);
1249 /* SIGHUP: set flag to re-read config file at next convenient time */
1251 WalSndSigHupHandler(SIGNAL_ARGS)
1253 int save_errno = errno;
1257 SetLatch(&MyWalSnd->latch);
1262 /* SIGTERM: set flag to shut down */
1264 WalSndShutdownHandler(SIGNAL_ARGS)
1266 int save_errno = errno;
1268 walsender_shutdown_requested = true;
1270 SetLatch(&MyWalSnd->latch);
1273 * Set the standard (non-walsender) state as well, so that we can abort
1274 * things like do_pg_stop_backup().
1276 InterruptPending = true;
1277 ProcDiePending = true;
1283 * WalSndQuickDieHandler() occurs when signalled SIGQUIT by the postmaster.
1285 * Some backend has bought the farm,
1286 * so we need to stop what we're doing and exit.
1289 WalSndQuickDieHandler(SIGNAL_ARGS)
1291 PG_SETMASK(&BlockSig);
1294 * We DO NOT want to run proc_exit() callbacks -- we're here because
1295 * shared memory may be corrupted, so we don't want to try to clean up our
1296 * transaction. Just nail the windows shut and get out of town. Now that
1297 * there's an atexit callback to prevent third-party code from breaking
1298 * things by calling exit() directly, we have to reset the callbacks
1299 * explicitly to make this work as intended.
1304 * Note we do exit(2) not exit(0). This is to force the postmaster into a
1305 * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
1306 * backend. This is necessary precisely because we don't clean up our
1307 * shared memory state. (The "dead man switch" mechanism in pmsignal.c
1308 * should ensure the postmaster sees this as a crash, too, but no harm in
1309 * being doubly sure.)
1314 /* SIGUSR1: set flag to send WAL records */
1316 WalSndXLogSendHandler(SIGNAL_ARGS)
1318 int save_errno = errno;
1320 latch_sigusr1_handler();
1325 /* SIGUSR2: set flag to do a last cycle and shut down afterwards */
1327 WalSndLastCycleHandler(SIGNAL_ARGS)
1329 int save_errno = errno;
1331 walsender_ready_to_stop = true;
1333 SetLatch(&MyWalSnd->latch);
1338 /* Set up signal handlers */
1342 /* Set up signal handlers */
1343 pqsignal(SIGHUP, WalSndSigHupHandler); /* set flag to read config
1345 pqsignal(SIGINT, SIG_IGN); /* not used */
1346 pqsignal(SIGTERM, WalSndShutdownHandler); /* request shutdown */
1347 pqsignal(SIGQUIT, WalSndQuickDieHandler); /* hard crash time */
1348 pqsignal(SIGALRM, SIG_IGN);
1349 pqsignal(SIGPIPE, SIG_IGN);
1350 pqsignal(SIGUSR1, WalSndXLogSendHandler); /* request WAL sending */
1351 pqsignal(SIGUSR2, WalSndLastCycleHandler); /* request a last cycle and
1354 /* Reset some signals that are accepted by postmaster but not here */
1355 pqsignal(SIGCHLD, SIG_DFL);
1356 pqsignal(SIGTTIN, SIG_DFL);
1357 pqsignal(SIGTTOU, SIG_DFL);
1358 pqsignal(SIGCONT, SIG_DFL);
1359 pqsignal(SIGWINCH, SIG_DFL);
1362 /* Report shared-memory space needed by WalSndShmemInit */
1364 WalSndShmemSize(void)
1368 size = offsetof(WalSndCtlData, walsnds);
1369 size = add_size(size, mul_size(max_wal_senders, sizeof(WalSnd)));
1374 /* Allocate and initialize walsender-related shared memory */
1376 WalSndShmemInit(void)
1381 WalSndCtl = (WalSndCtlData *)
1382 ShmemInitStruct("Wal Sender Ctl", WalSndShmemSize(), &found);
1386 /* First time through, so initialize */
1387 MemSet(WalSndCtl, 0, WalSndShmemSize());
1389 for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
1390 SHMQueueInit(&(WalSndCtl->SyncRepQueue[i]));
1392 for (i = 0; i < max_wal_senders; i++)
1394 WalSnd *walsnd = &WalSndCtl->walsnds[i];
1396 SpinLockInit(&walsnd->mutex);
1397 InitSharedLatch(&walsnd->latch);
1403 * Wake up all walsenders
1405 * This will be called inside critical sections, so throwing an error is not
1413 for (i = 0; i < max_wal_senders; i++)
1414 SetLatch(&WalSndCtl->walsnds[i].latch);
1417 /* Set state for current walsender (only called in walsender) */
1419 WalSndSetState(WalSndState state)
1421 /* use volatile pointer to prevent code rearrangement */
1422 volatile WalSnd *walsnd = MyWalSnd;
1424 Assert(am_walsender);
1426 if (walsnd->state == state)
1429 SpinLockAcquire(&walsnd->mutex);
1430 walsnd->state = state;
1431 SpinLockRelease(&walsnd->mutex);
1435 * Return a string constant representing the state. This is used
1436 * in system views, and should *not* be translated.
1439 WalSndGetStateString(WalSndState state)
1443 case WALSNDSTATE_STARTUP:
1445 case WALSNDSTATE_BACKUP:
1447 case WALSNDSTATE_CATCHUP:
1449 case WALSNDSTATE_STREAMING:
1457 * Returns activity of walsenders, including pids and xlog locations sent to
1461 pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
1463 #define PG_STAT_GET_WAL_SENDERS_COLS 8
1464 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1466 Tuplestorestate *tupstore;
1467 MemoryContext per_query_ctx;
1468 MemoryContext oldcontext;
1471 int sync_standby = -1;
1474 /* check to see if caller supports us returning a tuplestore */
1475 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1477 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1478 errmsg("set-valued function called in context that cannot accept a set")));
1479 if (!(rsinfo->allowedModes & SFRM_Materialize))
1481 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1482 errmsg("materialize mode required, but it is not " \
1483 "allowed in this context")));
1485 /* Build a tuple descriptor for our result type */
1486 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1487 elog(ERROR, "return type must be a row type");
1489 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1490 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1492 tupstore = tuplestore_begin_heap(true, false, work_mem);
1493 rsinfo->returnMode = SFRM_Materialize;
1494 rsinfo->setResult = tupstore;
1495 rsinfo->setDesc = tupdesc;
1497 MemoryContextSwitchTo(oldcontext);
1500 * Get the priorities of sync standbys all in one go, to minimise lock
1501 * acquisitions and to allow us to evaluate who is the current sync
1502 * standby. This code must match the code in SyncRepReleaseWaiters().
1504 sync_priority = palloc(sizeof(int) * max_wal_senders);
1505 LWLockAcquire(SyncRepLock, LW_SHARED);
1506 for (i = 0; i < max_wal_senders; i++)
1508 /* use volatile pointer to prevent code rearrangement */
1509 volatile WalSnd *walsnd = &WalSndCtl->walsnds[i];
1511 if (walsnd->pid != 0)
1513 sync_priority[i] = walsnd->sync_standby_priority;
1515 if (walsnd->state == WALSNDSTATE_STREAMING &&
1516 walsnd->sync_standby_priority > 0 &&
1518 priority > walsnd->sync_standby_priority))
1520 priority = walsnd->sync_standby_priority;
1525 LWLockRelease(SyncRepLock);
1527 for (i = 0; i < max_wal_senders; i++)
1529 /* use volatile pointer to prevent code rearrangement */
1530 volatile WalSnd *walsnd = &WalSndCtl->walsnds[i];
1531 char location[MAXFNAMELEN];
1537 Datum values[PG_STAT_GET_WAL_SENDERS_COLS];
1538 bool nulls[PG_STAT_GET_WAL_SENDERS_COLS];
1540 if (walsnd->pid == 0)
1543 SpinLockAcquire(&walsnd->mutex);
1544 sentPtr = walsnd->sentPtr;
1545 state = walsnd->state;
1546 write = walsnd->write;
1547 flush = walsnd->flush;
1548 apply = walsnd->apply;
1549 SpinLockRelease(&walsnd->mutex);
1551 memset(nulls, 0, sizeof(nulls));
1552 values[0] = Int32GetDatum(walsnd->pid);
1557 * Only superusers can see details. Other users only get the pid
1558 * value to know it's a walsender, but no details.
1560 MemSet(&nulls[1], true, PG_STAT_GET_WAL_SENDERS_COLS - 1);
1564 values[1] = CStringGetTextDatum(WalSndGetStateString(state));
1566 snprintf(location, sizeof(location), "%X/%X",
1567 (uint32) (sentPtr >> 32), (uint32) sentPtr);
1568 values[2] = CStringGetTextDatum(location);
1572 snprintf(location, sizeof(location), "%X/%X",
1573 (uint32) (write >> 32), (uint32) write);
1574 values[3] = CStringGetTextDatum(location);
1578 snprintf(location, sizeof(location), "%X/%X",
1579 (uint32) (flush >> 32), (uint32) flush);
1580 values[4] = CStringGetTextDatum(location);
1584 snprintf(location, sizeof(location), "%X/%X",
1585 (uint32) (apply >> 32), (uint32) apply);
1586 values[5] = CStringGetTextDatum(location);
1588 values[6] = Int32GetDatum(sync_priority[i]);
1591 * More easily understood version of standby state. This is purely
1592 * informational, not different from priority.
1594 if (sync_priority[i] == 0)
1595 values[7] = CStringGetTextDatum("async");
1596 else if (i == sync_standby)
1597 values[7] = CStringGetTextDatum("sync");
1599 values[7] = CStringGetTextDatum("potential");
1602 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1604 pfree(sync_priority);
1606 /* clean up and return the tuplestore */
1607 tuplestore_donestoring(tupstore);
1613 WalSndKeepalive(char *msgbuf)
1615 PrimaryKeepaliveMessage keepalive_message;
1617 /* Construct a new message */
1618 keepalive_message.walEnd = sentPtr;
1619 keepalive_message.sendTime = GetCurrentTimestamp();
1621 elog(DEBUG2, "sending replication keepalive");
1623 /* Prepend with the message type and send it. */
1625 memcpy(msgbuf + 1, &keepalive_message, sizeof(PrimaryKeepaliveMessage));
1626 pq_putmessage_noblock('d', msgbuf, sizeof(PrimaryKeepaliveMessage) + 1);
1630 * This isn't currently used for anything. Monitoring tools might be
1631 * interested in the future, and we'll need something like this in the
1632 * future for synchronous replication.
1636 * Returns the oldest Send position among walsenders. Or InvalidXLogRecPtr
1640 GetOldestWALSendPointer(void)
1642 XLogRecPtr oldest = {0, 0};
1646 for (i = 0; i < max_wal_senders; i++)
1648 /* use volatile pointer to prevent code rearrangement */
1649 volatile WalSnd *walsnd = &WalSndCtl->walsnds[i];
1652 if (walsnd->pid == 0)
1655 SpinLockAcquire(&walsnd->mutex);
1656 recptr = walsnd->sentPtr;
1657 SpinLockRelease(&walsnd->mutex);
1659 if (recptr.xlogid == 0 && recptr.xrecoff == 0)
1662 if (!found || XLByteLT(recptr, oldest))