PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
walsender.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * walsender.c
4 *
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 *
11 * A walsender is similar to a regular backend, ie. there is a one-to-one
12 * relationship between a connection and a walsender process, but instead
13 * of processing SQL queries, it understands a small set of special
14 * replication-mode commands. The START_REPLICATION command begins streaming
15 * WAL to the client. While streaming, the walsender keeps reading XLOG
16 * records from the disk and sends them to the standby server over the
17 * COPY protocol, until either side ends the replication by exiting COPY
18 * mode (or until the connection is closed).
19 *
20 * Normal termination is by SIGTERM, which instructs the walsender to
21 * close the connection and exit(0) at the next convenient moment. Emergency
22 * termination is by SIGQUIT; like any backend, the walsender will simply
23 * abort and exit on SIGQUIT. A close of the connection and a FATAL error
24 * are treated as not a crash but approximately normal termination;
25 * the walsender will exit quickly without sending any more XLOG records.
26 *
27 * If the server is shut down, checkpointer sends us
28 * PROCSIG_WALSND_INIT_STOPPING after all regular backends have exited. If
29 * the backend is idle or runs an SQL query this causes the backend to
30 * shutdown, if logical replication is in progress all existing WAL records
31 * are processed followed by a shutdown. Otherwise this causes the walsender
32 * to switch to the "stopping" state. In this state, the walsender will reject
33 * any further replication commands. The checkpointer begins the shutdown
34 * checkpoint once all walsenders are confirmed as stopping. When the shutdown
35 * checkpoint finishes, the postmaster sends us SIGUSR2. This instructs
36 * walsender to send any outstanding WAL, including the shutdown checkpoint
37 * record, wait for it to be replicated to the standby, and then exit.
38 *
39 *
40 * Portions Copyright (c) 2010-2025, PostgreSQL Global Development Group
41 *
42 * IDENTIFICATION
43 * src/backend/replication/walsender.c
44 *
45 *-------------------------------------------------------------------------
46 */
47#include "postgres.h"
48
49#include <signal.h>
50#include <unistd.h>
51
52#include "access/timeline.h"
53#include "access/transam.h"
54#include "access/xact.h"
56#include "access/xlogreader.h"
57#include "access/xlogrecovery.h"
58#include "access/xlogutils.h"
59#include "backup/basebackup.h"
61#include "catalog/pg_authid.h"
62#include "catalog/pg_type.h"
63#include "commands/dbcommands.h"
64#include "commands/defrem.h"
65#include "funcapi.h"
66#include "libpq/libpq.h"
67#include "libpq/pqformat.h"
68#include "miscadmin.h"
69#include "nodes/replnodes.h"
70#include "pgstat.h"
72#include "replication/decode.h"
73#include "replication/logical.h"
75#include "replication/slot.h"
77#include "replication/syncrep.h"
82#include "storage/aio_subsys.h"
83#include "storage/fd.h"
84#include "storage/ipc.h"
85#include "storage/pmsignal.h"
86#include "storage/proc.h"
87#include "tcop/dest.h"
88#include "tcop/tcopprot.h"
89#include "utils/acl.h"
90#include "utils/builtins.h"
91#include "utils/guc.h"
92#include "utils/memutils.h"
93#include "utils/pg_lsn.h"
95#include "utils/ps_status.h"
96#include "utils/timeout.h"
97#include "utils/timestamp.h"
98
99/* Minimum interval used by walsender for stats flushes, in ms */
100#define WALSENDER_STATS_FLUSH_INTERVAL 1000
101
102/*
103 * Maximum data payload in a WAL data message. Must be >= XLOG_BLCKSZ.
104 *
105 * We don't have a good idea of what a good value would be; there's some
106 * overhead per message in both walsender and walreceiver, but on the other
107 * hand sending large batches makes walsender less responsive to signals
108 * because signals are checked only between messages. 128kB (with
109 * default 8k blocks) seems like a reasonable guess for now.
110 */
111#define MAX_SEND_SIZE (XLOG_BLCKSZ * 16)
112
113/* Array of WalSnds in shared memory */
115
116/* My slot in the shared memory array */
118
119/* Global state */
120bool am_walsender = false; /* Am I a walsender process? */
121bool am_cascading_walsender = false; /* Am I cascading WAL to another
122 * standby? */
123bool am_db_walsender = false; /* Connected to a database? */
124
125/* GUC variables */
126int max_wal_senders = 10; /* the maximum number of concurrent
127 * walsenders */
128int wal_sender_timeout = 60 * 1000; /* maximum time to send one WAL
129 * data message */
131
132/*
133 * State for WalSndWakeupRequest
134 */
135bool wake_wal_senders = false;
136
137/*
138 * xlogreader used for replication. Note that a WAL sender doing physical
139 * replication does not need xlogreader to read WAL, but it needs one to
140 * keep a state of its work.
141 */
143
144/*
145 * If the UPLOAD_MANIFEST command is used to provide a backup manifest in
146 * preparation for an incremental backup, uploaded_manifest will be point
147 * to an object containing information about its contexts, and
148 * uploaded_manifest_mcxt will point to the memory context that contains
149 * that object and all of its subordinate data. Otherwise, both values will
150 * be NULL.
151 */
154
155/*
156 * These variables keep track of the state of the timeline we're currently
157 * sending. sendTimeLine identifies the timeline. If sendTimeLineIsHistoric,
158 * the timeline is not the latest timeline on this server, and the server's
159 * history forked off from that timeline at sendTimeLineValidUpto.
160 */
163static bool sendTimeLineIsHistoric = false;
165
166/*
167 * How far have we sent WAL already? This is also advertised in
168 * MyWalSnd->sentPtr. (Actually, this is the next WAL location to send.)
169 */
171
172/* Buffers for constructing outgoing messages and processing reply messages. */
176
177/* Timestamp of last ProcessRepliesIfAny(). */
179
180/*
181 * Timestamp of last ProcessRepliesIfAny() that saw a reply from the
182 * standby. Set to 0 if wal_sender_timeout doesn't need to be active.
183 */
185
186/* Have we sent a heartbeat message asking for reply, since last reply? */
187static bool waiting_for_ping_response = false;
188
189/*
190 * While streaming WAL in Copy mode, streamingDoneSending is set to true
191 * after we have sent CopyDone. We should not send any more CopyData messages
192 * after that. streamingDoneReceiving is set to true when we receive CopyDone
193 * from the other end. When both become true, it's time to exit Copy mode.
194 */
197
198/* Are we there yet? */
199static bool WalSndCaughtUp = false;
200
201/* Flags set by signal handlers for later service in main loop */
202static volatile sig_atomic_t got_SIGUSR2 = false;
203static volatile sig_atomic_t got_STOPPING = false;
204
205/*
206 * This is set while we are streaming. When not set
207 * PROCSIG_WALSND_INIT_STOPPING signal will be handled like SIGTERM. When set,
208 * the main loop is responsible for checking got_STOPPING and terminating when
209 * it's set (after streaming any remaining WAL).
210 */
211static volatile sig_atomic_t replication_active = false;
212
214
215/* A sample associating a WAL location with the time it was written. */
216typedef struct
217{
221
222/* The size of our buffer of time samples. */
223#define LAG_TRACKER_BUFFER_SIZE 8192
224
225/* A mechanism for tracking replication lag. */
226typedef struct
227{
231 int read_heads[NUM_SYNC_REP_WAIT_MODE];
233} LagTracker;
234
236
237/* Signal handlers */
239
240/* Prototypes for private functions */
241typedef void (*WalSndSendDataCallback) (void);
242static void WalSndLoop(WalSndSendDataCallback send_data);
243static void InitWalSenderSlot(void);
244static void WalSndKill(int code, Datum arg);
245pg_noreturn static void WalSndShutdown(void);
246static void XLogSendPhysical(void);
247static void XLogSendLogical(void);
248static void WalSndDone(WalSndSendDataCallback send_data);
249static void IdentifySystem(void);
250static void UploadManifest(void);
251static bool HandleUploadManifestPacket(StringInfo buf, off_t *offset,
256static void StartReplication(StartReplicationCmd *cmd);
258static void ProcessStandbyMessage(void);
259static void ProcessStandbyReplyMessage(void);
260static void ProcessStandbyHSFeedbackMessage(void);
261static void ProcessRepliesIfAny(void);
262static void ProcessPendingWrites(void);
263static void WalSndKeepalive(bool requestReply, XLogRecPtr writePtr);
264static void WalSndKeepaliveIfNecessary(void);
265static void WalSndCheckTimeOut(void);
267static void WalSndWait(uint32 socket_events, long timeout, uint32 wait_event);
268static void WalSndPrepareWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write);
269static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write);
271 bool skipped_xact);
273static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time);
274static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now);
276
277static void WalSndSegmentOpen(XLogReaderState *state, XLogSegNo nextSegNo,
278 TimeLineID *tli_p);
279
280
281/* Initialize walsender process before entering the main command loop */
282void
284{
286
287 /* Create a per-walsender data structure in shared memory */
289
290 /* need resource owner for e.g. basebackups */
292
293 /*
294 * Let postmaster know that we're a WAL sender. Once we've declared us as
295 * a WAL sender process, postmaster will let us outlive the bgwriter and
296 * kill us last in the shutdown sequence, so we get a chance to stream all
297 * remaining WAL at shutdown, including the shutdown checkpoint. Note that
298 * there's no going back, and we mustn't write any WAL records after this.
299 */
302
303 /*
304 * If the client didn't specify a database to connect to, show in PGPROC
305 * that our advertised xmin should affect vacuum horizons in all
306 * databases. This allows physical replication clients to send hot
307 * standby feedback that will delay vacuum cleanup in all databases.
308 */
310 {
312 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
315 LWLockRelease(ProcArrayLock);
316 }
317
318 /* Initialize empty timestamp buffer for lag tracking. */
320}
321
322/*
323 * Clean up after an error.
324 *
325 * WAL sender processes don't use transactions like regular backends do.
326 * This function does any cleanup required after an error in a WAL sender
327 * process, similar to what transaction abort does in a regular backend.
328 */
329void
331{
336
337 if (xlogreader != NULL && xlogreader->seg.ws_file >= 0)
339
340 if (MyReplicationSlot != NULL)
342
344
345 replication_active = false;
346
347 /*
348 * If there is a transaction in progress, it will clean up our
349 * ResourceOwner, but if a replication command set up a resource owner
350 * without a transaction, we've got to clean that up now.
351 */
354
356 proc_exit(0);
357
358 /* Revert back to startup state */
360}
361
362/*
363 * Handle a client's connection abort in an orderly manner.
364 */
365static void
367{
368 /*
369 * Reset whereToSendOutput to prevent ereport from attempting to send any
370 * more messages to the standby.
371 */
374
375 proc_exit(0);
376 abort(); /* keep the compiler quiet */
377}
378
379/*
380 * Handle the IDENTIFY_SYSTEM command.
381 */
382static void
384{
385 char sysid[32];
386 char xloc[MAXFNAMELEN];
387 XLogRecPtr logptr;
388 char *dbname = NULL;
390 TupOutputState *tstate;
391 TupleDesc tupdesc;
392 Datum values[4];
393 bool nulls[4] = {0};
394 TimeLineID currTLI;
395
396 /*
397 * Reply with a result set with one row, four columns. First col is system
398 * ID, second is timeline ID, third is current xlog location and the
399 * fourth contains the database name if we are connected to one.
400 */
401
402 snprintf(sysid, sizeof(sysid), UINT64_FORMAT,
404
407 logptr = GetStandbyFlushRecPtr(&currTLI);
408 else
409 logptr = GetFlushRecPtr(&currTLI);
410
411 snprintf(xloc, sizeof(xloc), "%X/%X", LSN_FORMAT_ARGS(logptr));
412
414 {
416
417 /* syscache access needs a transaction env. */
420 /* copy dbname out of TX context */
423 }
424
426
427 /* need a tuple descriptor representing four columns */
428 tupdesc = CreateTemplateTupleDesc(4);
429 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "systemid",
430 TEXTOID, -1, 0);
431 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "timeline",
432 INT8OID, -1, 0);
433 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 3, "xlogpos",
434 TEXTOID, -1, 0);
435 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 4, "dbname",
436 TEXTOID, -1, 0);
437
438 /* prepare for projection of tuples */
439 tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
440
441 /* column 1: system identifier */
442 values[0] = CStringGetTextDatum(sysid);
443
444 /* column 2: timeline */
445 values[1] = Int64GetDatum(currTLI);
446
447 /* column 3: wal location */
448 values[2] = CStringGetTextDatum(xloc);
449
450 /* column 4: database name, or NULL if none */
451 if (dbname)
453 else
454 nulls[3] = true;
455
456 /* send it to dest */
457 do_tup_output(tstate, values, nulls);
458
459 end_tup_output(tstate);
460}
461
462/* Handle READ_REPLICATION_SLOT command */
463static void
465{
466#define READ_REPLICATION_SLOT_COLS 3
467 ReplicationSlot *slot;
469 TupOutputState *tstate;
470 TupleDesc tupdesc;
472 bool nulls[READ_REPLICATION_SLOT_COLS];
473
475 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "slot_type",
476 TEXTOID, -1, 0);
477 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "restart_lsn",
478 TEXTOID, -1, 0);
479 /* TimeLineID is unsigned, so int4 is not wide enough. */
480 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 3, "restart_tli",
481 INT8OID, -1, 0);
482
483 memset(nulls, true, READ_REPLICATION_SLOT_COLS * sizeof(bool));
484
485 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
486 slot = SearchNamedReplicationSlot(cmd->slotname, false);
487 if (slot == NULL || !slot->in_use)
488 {
489 LWLockRelease(ReplicationSlotControlLock);
490 }
491 else
492 {
493 ReplicationSlot slot_contents;
494 int i = 0;
495
496 /* Copy slot contents while holding spinlock */
497 SpinLockAcquire(&slot->mutex);
498 slot_contents = *slot;
499 SpinLockRelease(&slot->mutex);
500 LWLockRelease(ReplicationSlotControlLock);
501
502 if (OidIsValid(slot_contents.data.database))
504 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
505 errmsg("cannot use %s with a logical replication slot",
506 "READ_REPLICATION_SLOT"));
507
508 /* slot type */
509 values[i] = CStringGetTextDatum("physical");
510 nulls[i] = false;
511 i++;
512
513 /* start LSN */
514 if (!XLogRecPtrIsInvalid(slot_contents.data.restart_lsn))
515 {
516 char xloc[64];
517
518 snprintf(xloc, sizeof(xloc), "%X/%X",
519 LSN_FORMAT_ARGS(slot_contents.data.restart_lsn));
521 nulls[i] = false;
522 }
523 i++;
524
525 /* timeline this WAL was produced on */
526 if (!XLogRecPtrIsInvalid(slot_contents.data.restart_lsn))
527 {
528 TimeLineID slots_position_timeline;
529 TimeLineID current_timeline;
530 List *timeline_history = NIL;
531
532 /*
533 * While in recovery, use as timeline the currently-replaying one
534 * to get the LSN position's history.
535 */
536 if (RecoveryInProgress())
537 (void) GetXLogReplayRecPtr(&current_timeline);
538 else
539 current_timeline = GetWALInsertionTimeLine();
540
541 timeline_history = readTimeLineHistory(current_timeline);
542 slots_position_timeline = tliOfPointInHistory(slot_contents.data.restart_lsn,
543 timeline_history);
544 values[i] = Int64GetDatum((int64) slots_position_timeline);
545 nulls[i] = false;
546 }
547 i++;
548
550 }
551
553 tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
554 do_tup_output(tstate, values, nulls);
555 end_tup_output(tstate);
556}
557
558
559/*
560 * Handle TIMELINE_HISTORY command.
561 */
562static void
564{
566 TupleDesc tupdesc;
568 char histfname[MAXFNAMELEN];
569 char path[MAXPGPATH];
570 int fd;
571 off_t histfilelen;
572 off_t bytesleft;
573 Size len;
574
576
577 /*
578 * Reply with a result set with one row, and two columns. The first col is
579 * the name of the history file, 2nd is the contents.
580 */
581 tupdesc = CreateTemplateTupleDesc(2);
582 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "filename", TEXTOID, -1, 0);
583 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "content", TEXTOID, -1, 0);
584
585 TLHistoryFileName(histfname, cmd->timeline);
586 TLHistoryFilePath(path, cmd->timeline);
587
588 /* Send a RowDescription message */
589 dest->rStartup(dest, CMD_SELECT, tupdesc);
590
591 /* Send a DataRow message */
593 pq_sendint16(&buf, 2); /* # of columns */
594 len = strlen(histfname);
595 pq_sendint32(&buf, len); /* col1 len */
596 pq_sendbytes(&buf, histfname, len);
597
598 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
599 if (fd < 0)
602 errmsg("could not open file \"%s\": %m", path)));
603
604 /* Determine file length and send it to client */
605 histfilelen = lseek(fd, 0, SEEK_END);
606 if (histfilelen < 0)
609 errmsg("could not seek to end of file \"%s\": %m", path)));
610 if (lseek(fd, 0, SEEK_SET) != 0)
613 errmsg("could not seek to beginning of file \"%s\": %m", path)));
614
615 pq_sendint32(&buf, histfilelen); /* col2 len */
616
617 bytesleft = histfilelen;
618 while (bytesleft > 0)
619 {
620 PGAlignedBlock rbuf;
621 int nread;
622
623 pgstat_report_wait_start(WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ);
624 nread = read(fd, rbuf.data, sizeof(rbuf));
626 if (nread < 0)
629 errmsg("could not read file \"%s\": %m",
630 path)));
631 else if (nread == 0)
634 errmsg("could not read file \"%s\": read %d of %zu",
635 path, nread, (Size) bytesleft)));
636
637 pq_sendbytes(&buf, rbuf.data, nread);
638 bytesleft -= nread;
639 }
640
641 if (CloseTransientFile(fd) != 0)
644 errmsg("could not close file \"%s\": %m", path)));
645
647}
648
649/*
650 * Handle UPLOAD_MANIFEST command.
651 */
652static void
654{
655 MemoryContext mcxt;
657 off_t offset = 0;
659
660 /*
661 * parsing the manifest will use the cryptohash stuff, which requires a
662 * resource owner
663 */
666 CurrentResourceOwner == NULL);
668
669 /* Prepare to read manifest data into a temporary context. */
671 "incremental backup information",
674
675 /* Send a CopyInResponse message */
677 pq_sendbyte(&buf, 0);
678 pq_sendint16(&buf, 0);
680 pq_flush();
681
682 /* Receive packets from client until done. */
683 while (HandleUploadManifestPacket(&buf, &offset, ib))
684 ;
685
686 /* Finish up manifest processing. */
688
689 /*
690 * Discard any old manifest information and arrange to preserve the new
691 * information we just got.
692 *
693 * We assume that MemoryContextDelete and MemoryContextSetParent won't
694 * fail, and thus we shouldn't end up bailing out of here in such a way as
695 * to leave dangling pointers.
696 */
697 if (uploaded_manifest_mcxt != NULL)
702
703 /* clean up the resource owner we created */
705}
706
707/*
708 * Process one packet received during the handling of an UPLOAD_MANIFEST
709 * operation.
710 *
711 * 'buf' is scratch space. This function expects it to be initialized, doesn't
712 * care what the current contents are, and may override them with completely
713 * new contents.
714 *
715 * The return value is true if the caller should continue processing
716 * additional packets and false if the UPLOAD_MANIFEST operation is complete.
717 */
718static bool
721{
722 int mtype;
723 int maxmsglen;
724
726
728 mtype = pq_getbyte();
729 if (mtype == EOF)
731 (errcode(ERRCODE_CONNECTION_FAILURE),
732 errmsg("unexpected EOF on client connection with an open transaction")));
733
734 switch (mtype)
735 {
736 case 'd': /* CopyData */
737 maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
738 break;
739 case 'c': /* CopyDone */
740 case 'f': /* CopyFail */
741 case 'H': /* Flush */
742 case 'S': /* Sync */
743 maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
744 break;
745 default:
747 (errcode(ERRCODE_PROTOCOL_VIOLATION),
748 errmsg("unexpected message type 0x%02X during COPY from stdin",
749 mtype)));
750 maxmsglen = 0; /* keep compiler quiet */
751 break;
752 }
753
754 /* Now collect the message body */
755 if (pq_getmessage(buf, maxmsglen))
757 (errcode(ERRCODE_CONNECTION_FAILURE),
758 errmsg("unexpected EOF on client connection with an open transaction")));
760
761 /* Process the message */
762 switch (mtype)
763 {
764 case 'd': /* CopyData */
765 AppendIncrementalManifestData(ib, buf->data, buf->len);
766 return true;
767
768 case 'c': /* CopyDone */
769 return false;
770
771 case 'H': /* Sync */
772 case 'S': /* Flush */
773 /* Ignore these while in CopyOut mode as we do elsewhere. */
774 return true;
775
776 case 'f':
778 (errcode(ERRCODE_QUERY_CANCELED),
779 errmsg("COPY from stdin failed: %s",
781 }
782
783 /* Not reached. */
784 Assert(false);
785 return false;
786}
787
788/*
789 * Handle START_REPLICATION command.
790 *
791 * At the moment, this never returns, but an ereport(ERROR) will take us back
792 * to the main loop.
793 */
794static void
796{
798 XLogRecPtr FlushPtr;
799 TimeLineID FlushTLI;
800
801 /* create xlogreader for physical replication */
802 xlogreader =
804 XL_ROUTINE(.segment_open = WalSndSegmentOpen,
805 .segment_close = wal_segment_close),
806 NULL);
807
808 if (!xlogreader)
810 (errcode(ERRCODE_OUT_OF_MEMORY),
811 errmsg("out of memory"),
812 errdetail("Failed while allocating a WAL reading processor.")));
813
814 /*
815 * We assume here that we're logging enough information in the WAL for
816 * log-shipping, since this is checked in PostmasterMain().
817 *
818 * NOTE: wal_level can only change at shutdown, so in most cases it is
819 * difficult for there to be WAL data that we can still see that was
820 * written at wal_level='minimal'.
821 */
822
823 if (cmd->slotname)
824 {
825 ReplicationSlotAcquire(cmd->slotname, true, true);
828 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
829 errmsg("cannot use a logical replication slot for physical replication")));
830
831 /*
832 * We don't need to verify the slot's restart_lsn here; instead we
833 * rely on the caller requesting the starting point to use. If the
834 * WAL segment doesn't exist, we'll fail later.
835 */
836 }
837
838 /*
839 * Select the timeline. If it was given explicitly by the client, use
840 * that. Otherwise use the timeline of the last replayed record.
841 */
844 FlushPtr = GetStandbyFlushRecPtr(&FlushTLI);
845 else
846 FlushPtr = GetFlushRecPtr(&FlushTLI);
847
848 if (cmd->timeline != 0)
849 {
850 XLogRecPtr switchpoint;
851
852 sendTimeLine = cmd->timeline;
853 if (sendTimeLine == FlushTLI)
854 {
857 }
858 else
859 {
860 List *timeLineHistory;
861
863
864 /*
865 * Check that the timeline the client requested exists, and the
866 * requested start location is on that timeline.
867 */
868 timeLineHistory = readTimeLineHistory(FlushTLI);
869 switchpoint = tliSwitchPoint(cmd->timeline, timeLineHistory,
871 list_free_deep(timeLineHistory);
872
873 /*
874 * Found the requested timeline in the history. Check that
875 * requested startpoint is on that timeline in our history.
876 *
877 * This is quite loose on purpose. We only check that we didn't
878 * fork off the requested timeline before the switchpoint. We
879 * don't check that we switched *to* it before the requested
880 * starting point. This is because the client can legitimately
881 * request to start replication from the beginning of the WAL
882 * segment that contains switchpoint, but on the new timeline, so
883 * that it doesn't end up with a partial segment. If you ask for
884 * too old a starting point, you'll get an error later when we
885 * fail to find the requested WAL segment in pg_wal.
886 *
887 * XXX: we could be more strict here and only allow a startpoint
888 * that's older than the switchpoint, if it's still in the same
889 * WAL segment.
890 */
891 if (!XLogRecPtrIsInvalid(switchpoint) &&
892 switchpoint < cmd->startpoint)
893 {
895 (errmsg("requested starting point %X/%X on timeline %u is not in this server's history",
897 cmd->timeline),
898 errdetail("This server's history forked from timeline %u at %X/%X.",
899 cmd->timeline,
900 LSN_FORMAT_ARGS(switchpoint))));
901 }
902 sendTimeLineValidUpto = switchpoint;
903 }
904 }
905 else
906 {
907 sendTimeLine = FlushTLI;
910 }
911
913
914 /* If there is nothing to stream, don't even enter COPY mode */
916 {
917 /*
918 * When we first start replication the standby will be behind the
919 * primary. For some applications, for example synchronous
920 * replication, it is important to have a clear state for this initial
921 * catchup mode, so we can trigger actions when we change streaming
922 * state later. We may stay in this state for a long time, which is
923 * exactly why we want to be able to monitor whether or not we are
924 * still here.
925 */
927
928 /* Send a CopyBothResponse message, and start streaming */
930 pq_sendbyte(&buf, 0);
931 pq_sendint16(&buf, 0);
933 pq_flush();
934
935 /*
936 * Don't allow a request to stream from a future point in WAL that
937 * hasn't been flushed to disk in this server yet.
938 */
939 if (FlushPtr < cmd->startpoint)
940 {
942 (errmsg("requested starting point %X/%X is ahead of the WAL flush position of this server %X/%X",
944 LSN_FORMAT_ARGS(FlushPtr))));
945 }
946
947 /* Start streaming from the requested point */
948 sentPtr = cmd->startpoint;
949
950 /* Initialize shared memory status, too */
954
956
957 /* Main loop of walsender */
958 replication_active = true;
959
961
962 replication_active = false;
963 if (got_STOPPING)
964 proc_exit(0);
966
968 }
969
970 if (cmd->slotname)
972
973 /*
974 * Copy is finished now. Send a single-row result set indicating the next
975 * timeline.
976 */
978 {
979 char startpos_str[8 + 1 + 8 + 1];
981 TupOutputState *tstate;
982 TupleDesc tupdesc;
983 Datum values[2];
984 bool nulls[2] = {0};
985
986 snprintf(startpos_str, sizeof(startpos_str), "%X/%X",
988
990
991 /*
992 * Need a tuple descriptor representing two columns. int8 may seem
993 * like a surprising data type for this, but in theory int4 would not
994 * be wide enough for this, as TimeLineID is unsigned.
995 */
996 tupdesc = CreateTemplateTupleDesc(2);
997 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "next_tli",
998 INT8OID, -1, 0);
999 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "next_tli_startpos",
1000 TEXTOID, -1, 0);
1001
1002 /* prepare for projection of tuple */
1003 tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
1004
1006 values[1] = CStringGetTextDatum(startpos_str);
1007
1008 /* send it to dest */
1009 do_tup_output(tstate, values, nulls);
1010
1011 end_tup_output(tstate);
1012 }
1013
1014 /* Send CommandComplete message */
1015 EndReplicationCommand("START_STREAMING");
1016}
1017
1018/*
1019 * XLogReaderRoutine->page_read callback for logical decoding contexts, as a
1020 * walsender process.
1021 *
1022 * Inside the walsender we can do better than read_local_xlog_page,
1023 * which has to do a plain sleep/busy loop, because the walsender's latch gets
1024 * set every time WAL is flushed.
1025 */
1026static int
1028 XLogRecPtr targetRecPtr, char *cur_page)
1029{
1030 XLogRecPtr flushptr;
1031 int count;
1032 WALReadError errinfo;
1033 XLogSegNo segno;
1034 TimeLineID currTLI;
1035
1036 /*
1037 * Make sure we have enough WAL available before retrieving the current
1038 * timeline.
1039 */
1040 flushptr = WalSndWaitForWal(targetPagePtr + reqLen);
1041
1042 /* Fail if not enough (implies we are going to shut down) */
1043 if (flushptr < targetPagePtr + reqLen)
1044 return -1;
1045
1046 /*
1047 * Since logical decoding is also permitted on a standby server, we need
1048 * to check if the server is in recovery to decide how to get the current
1049 * timeline ID (so that it also covers the promotion or timeline change
1050 * cases). We must determine am_cascading_walsender after waiting for the
1051 * required WAL so that it is correct when the walsender wakes up after a
1052 * promotion.
1053 */
1055
1057 GetXLogReplayRecPtr(&currTLI);
1058 else
1059 currTLI = GetWALInsertionTimeLine();
1060
1061 XLogReadDetermineTimeline(state, targetPagePtr, reqLen, currTLI);
1062 sendTimeLineIsHistoric = (state->currTLI != currTLI);
1063 sendTimeLine = state->currTLI;
1064 sendTimeLineValidUpto = state->currTLIValidUntil;
1065 sendTimeLineNextTLI = state->nextTLI;
1066
1067 if (targetPagePtr + XLOG_BLCKSZ <= flushptr)
1068 count = XLOG_BLCKSZ; /* more than one block available */
1069 else
1070 count = flushptr - targetPagePtr; /* part of the page available */
1071
1072 /* now actually read the data, we know it's there */
1073 if (!WALRead(state,
1074 cur_page,
1075 targetPagePtr,
1076 count,
1077 currTLI, /* Pass the current TLI because only
1078 * WalSndSegmentOpen controls whether new TLI
1079 * is needed. */
1080 &errinfo))
1081 WALReadRaiseError(&errinfo);
1082
1083 /*
1084 * After reading into the buffer, check that what we read was valid. We do
1085 * this after reading, because even though the segment was present when we
1086 * opened it, it might get recycled or removed while we read it. The
1087 * read() succeeds in that case, but the data we tried to read might
1088 * already have been overwritten with new WAL records.
1089 */
1090 XLByteToSeg(targetPagePtr, segno, state->segcxt.ws_segsize);
1091 CheckXLogRemoved(segno, state->seg.ws_tli);
1092
1093 return count;
1094}
1095
1096/*
1097 * Process extra options given to CREATE_REPLICATION_SLOT.
1098 */
1099static void
1101 bool *reserve_wal,
1102 CRSSnapshotAction *snapshot_action,
1103 bool *two_phase, bool *failover)
1104{
1105 ListCell *lc;
1106 bool snapshot_action_given = false;
1107 bool reserve_wal_given = false;
1108 bool two_phase_given = false;
1109 bool failover_given = false;
1110
1111 /* Parse options */
1112 foreach(lc, cmd->options)
1113 {
1114 DefElem *defel = (DefElem *) lfirst(lc);
1115
1116 if (strcmp(defel->defname, "snapshot") == 0)
1117 {
1118 char *action;
1119
1120 if (snapshot_action_given || cmd->kind != REPLICATION_KIND_LOGICAL)
1121 ereport(ERROR,
1122 (errcode(ERRCODE_SYNTAX_ERROR),
1123 errmsg("conflicting or redundant options")));
1124
1125 action = defGetString(defel);
1126 snapshot_action_given = true;
1127
1128 if (strcmp(action, "export") == 0)
1129 *snapshot_action = CRS_EXPORT_SNAPSHOT;
1130 else if (strcmp(action, "nothing") == 0)
1131 *snapshot_action = CRS_NOEXPORT_SNAPSHOT;
1132 else if (strcmp(action, "use") == 0)
1133 *snapshot_action = CRS_USE_SNAPSHOT;
1134 else
1135 ereport(ERROR,
1136 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1137 errmsg("unrecognized value for CREATE_REPLICATION_SLOT option \"%s\": \"%s\"",
1138 defel->defname, action)));
1139 }
1140 else if (strcmp(defel->defname, "reserve_wal") == 0)
1141 {
1142 if (reserve_wal_given || cmd->kind != REPLICATION_KIND_PHYSICAL)
1143 ereport(ERROR,
1144 (errcode(ERRCODE_SYNTAX_ERROR),
1145 errmsg("conflicting or redundant options")));
1146
1147 reserve_wal_given = true;
1148 *reserve_wal = defGetBoolean(defel);
1149 }
1150 else if (strcmp(defel->defname, "two_phase") == 0)
1151 {
1152 if (two_phase_given || cmd->kind != REPLICATION_KIND_LOGICAL)
1153 ereport(ERROR,
1154 (errcode(ERRCODE_SYNTAX_ERROR),
1155 errmsg("conflicting or redundant options")));
1156 two_phase_given = true;
1157 *two_phase = defGetBoolean(defel);
1158 }
1159 else if (strcmp(defel->defname, "failover") == 0)
1160 {
1161 if (failover_given || cmd->kind != REPLICATION_KIND_LOGICAL)
1162 ereport(ERROR,
1163 (errcode(ERRCODE_SYNTAX_ERROR),
1164 errmsg("conflicting or redundant options")));
1165 failover_given = true;
1166 *failover = defGetBoolean(defel);
1167 }
1168 else
1169 elog(ERROR, "unrecognized option: %s", defel->defname);
1170 }
1171}
1172
1173/*
1174 * Create a new replication slot.
1175 */
1176static void
1178{
1179 const char *snapshot_name = NULL;
1180 char xloc[MAXFNAMELEN];
1181 char *slot_name;
1182 bool reserve_wal = false;
1183 bool two_phase = false;
1184 bool failover = false;
1185 CRSSnapshotAction snapshot_action = CRS_EXPORT_SNAPSHOT;
1187 TupOutputState *tstate;
1188 TupleDesc tupdesc;
1189 Datum values[4];
1190 bool nulls[4] = {0};
1191
1193
1194 parseCreateReplSlotOptions(cmd, &reserve_wal, &snapshot_action, &two_phase,
1195 &failover);
1196
1197 if (cmd->kind == REPLICATION_KIND_PHYSICAL)
1198 {
1199 ReplicationSlotCreate(cmd->slotname, false,
1201 false, false, false);
1202
1203 if (reserve_wal)
1204 {
1206
1208
1209 /* Write this slot to disk if it's a permanent one. */
1210 if (!cmd->temporary)
1212 }
1213 }
1214 else
1215 {
1217 bool need_full_snapshot = false;
1218
1220
1222
1223 /*
1224 * Initially create persistent slot as ephemeral - that allows us to
1225 * nicely handle errors during initialization because it'll get
1226 * dropped if this transaction fails. We'll make it persistent at the
1227 * end. Temporary slots can be created as temporary from beginning as
1228 * they get dropped on error as well.
1229 */
1232 two_phase, failover, false);
1233
1234 /*
1235 * Do options check early so that we can bail before calling the
1236 * DecodingContextFindStartpoint which can take long time.
1237 */
1238 if (snapshot_action == CRS_EXPORT_SNAPSHOT)
1239 {
1240 if (IsTransactionBlock())
1241 ereport(ERROR,
1242 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1243 (errmsg("%s must not be called inside a transaction",
1244 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'export')")));
1245
1246 need_full_snapshot = true;
1247 }
1248 else if (snapshot_action == CRS_USE_SNAPSHOT)
1249 {
1250 if (!IsTransactionBlock())
1251 ereport(ERROR,
1252 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1253 (errmsg("%s must be called inside a transaction",
1254 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'use')")));
1255
1257 ereport(ERROR,
1258 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1259 (errmsg("%s must be called in REPEATABLE READ isolation mode transaction",
1260 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'use')")));
1261 if (!XactReadOnly)
1262 ereport(ERROR,
1263 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1264 (errmsg("%s must be called in a read-only transaction",
1265 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'use')")));
1266
1267 if (FirstSnapshotSet)
1268 ereport(ERROR,
1269 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1270 (errmsg("%s must be called before any query",
1271 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'use')")));
1272
1273 if (IsSubTransaction())
1274 ereport(ERROR,
1275 /*- translator: %s is a CREATE_REPLICATION_SLOT statement */
1276 (errmsg("%s must not be called in a subtransaction",
1277 "CREATE_REPLICATION_SLOT ... (SNAPSHOT 'use')")));
1278
1279 need_full_snapshot = true;
1280 }
1281
1282 ctx = CreateInitDecodingContext(cmd->plugin, NIL, need_full_snapshot,
1285 .segment_open = WalSndSegmentOpen,
1286 .segment_close = wal_segment_close),
1289
1290 /*
1291 * Signal that we don't need the timeout mechanism. We're just
1292 * creating the replication slot and don't yet accept feedback
1293 * messages or send keepalives. As we possibly need to wait for
1294 * further WAL the walsender would otherwise possibly be killed too
1295 * soon.
1296 */
1298
1299 /* build initial snapshot, might take a while */
1301
1302 /*
1303 * Export or use the snapshot if we've been asked to do so.
1304 *
1305 * NB. We will convert the snapbuild.c kind of snapshot to normal
1306 * snapshot when doing this.
1307 */
1308 if (snapshot_action == CRS_EXPORT_SNAPSHOT)
1309 {
1310 snapshot_name = SnapBuildExportSnapshot(ctx->snapshot_builder);
1311 }
1312 else if (snapshot_action == CRS_USE_SNAPSHOT)
1313 {
1314 Snapshot snap;
1315
1318 }
1319
1320 /* don't need the decoding context anymore */
1322
1323 if (!cmd->temporary)
1325 }
1326
1327 snprintf(xloc, sizeof(xloc), "%X/%X",
1329
1331
1332 /*----------
1333 * Need a tuple descriptor representing four columns:
1334 * - first field: the slot name
1335 * - second field: LSN at which we became consistent
1336 * - third field: exported snapshot's name
1337 * - fourth field: output plugin
1338 */
1339 tupdesc = CreateTemplateTupleDesc(4);
1340 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "slot_name",
1341 TEXTOID, -1, 0);
1342 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "consistent_point",
1343 TEXTOID, -1, 0);
1344 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 3, "snapshot_name",
1345 TEXTOID, -1, 0);
1346 TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 4, "output_plugin",
1347 TEXTOID, -1, 0);
1348
1349 /* prepare for projection of tuples */
1350 tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
1351
1352 /* slot_name */
1353 slot_name = NameStr(MyReplicationSlot->data.name);
1354 values[0] = CStringGetTextDatum(slot_name);
1355
1356 /* consistent wal location */
1357 values[1] = CStringGetTextDatum(xloc);
1358
1359 /* snapshot name, or NULL if none */
1360 if (snapshot_name != NULL)
1361 values[2] = CStringGetTextDatum(snapshot_name);
1362 else
1363 nulls[2] = true;
1364
1365 /* plugin, or NULL if none */
1366 if (cmd->plugin != NULL)
1368 else
1369 nulls[3] = true;
1370
1371 /* send it to dest */
1372 do_tup_output(tstate, values, nulls);
1373 end_tup_output(tstate);
1374
1376}
1377
1378/*
1379 * Get rid of a replication slot that is no longer wanted.
1380 */
1381static void
1383{
1384 ReplicationSlotDrop(cmd->slotname, !cmd->wait);
1385}
1386
1387/*
1388 * Change the definition of a replication slot.
1389 */
1390static void
1392{
1393 bool failover_given = false;
1394 bool two_phase_given = false;
1395 bool failover;
1396 bool two_phase;
1397
1398 /* Parse options */
1399 foreach_ptr(DefElem, defel, cmd->options)
1400 {
1401 if (strcmp(defel->defname, "failover") == 0)
1402 {
1403 if (failover_given)
1404 ereport(ERROR,
1405 (errcode(ERRCODE_SYNTAX_ERROR),
1406 errmsg("conflicting or redundant options")));
1407 failover_given = true;
1408 failover = defGetBoolean(defel);
1409 }
1410 else if (strcmp(defel->defname, "two_phase") == 0)
1411 {
1412 if (two_phase_given)
1413 ereport(ERROR,
1414 (errcode(ERRCODE_SYNTAX_ERROR),
1415 errmsg("conflicting or redundant options")));
1416 two_phase_given = true;
1417 two_phase = defGetBoolean(defel);
1418 }
1419 else
1420 elog(ERROR, "unrecognized option: %s", defel->defname);
1421 }
1422
1424 failover_given ? &failover : NULL,
1425 two_phase_given ? &two_phase : NULL);
1426}
1427
1428/*
1429 * Load previously initiated logical slot and prepare for sending data (via
1430 * WalSndLoop).
1431 */
1432static void
1434{
1436 QueryCompletion qc;
1437
1438 /* make sure that our requirements are still fulfilled */
1440
1442
1443 ReplicationSlotAcquire(cmd->slotname, true, true);
1444
1445 /*
1446 * Force a disconnect, so that the decoding code doesn't need to care
1447 * about an eventual switch from running in recovery, to running in a
1448 * normal environment. Client code is expected to handle reconnects.
1449 */
1451 {
1452 ereport(LOG,
1453 (errmsg("terminating walsender process after promotion")));
1454 got_STOPPING = true;
1455 }
1456
1457 /*
1458 * Create our decoding context, making it start at the previously ack'ed
1459 * position.
1460 *
1461 * Do this before sending a CopyBothResponse message, so that any errors
1462 * are reported early.
1463 */
1465 CreateDecodingContext(cmd->startpoint, cmd->options, false,
1467 .segment_open = WalSndSegmentOpen,
1468 .segment_close = wal_segment_close),
1472
1474
1475 /* Send a CopyBothResponse message, and start streaming */
1477 pq_sendbyte(&buf, 0);
1478 pq_sendint16(&buf, 0);
1480 pq_flush();
1481
1482 /* Start reading WAL from the oldest required WAL. */
1485
1486 /*
1487 * Report the location after which we'll send out further commits as the
1488 * current sentPtr.
1489 */
1491
1492 /* Also update the sent position status in shared memory */
1496
1497 replication_active = true;
1498
1500
1501 /* Main loop of walsender */
1503
1506
1507 replication_active = false;
1508 if (got_STOPPING)
1509 proc_exit(0);
1511
1512 /* Get out of COPY mode (CommandComplete). */
1513 SetQueryCompletion(&qc, CMDTAG_COPY, 0);
1514 EndCommand(&qc, DestRemote, false);
1515}
1516
1517/*
1518 * LogicalDecodingContext 'prepare_write' callback.
1519 *
1520 * Prepare a write into a StringInfo.
1521 *
1522 * Don't do anything lasting in here, it's quite possible that nothing will be done
1523 * with the data.
1524 */
1525static void
1527{
1528 /* can't have sync rep confused by sending the same LSN several times */
1529 if (!last_write)
1530 lsn = InvalidXLogRecPtr;
1531
1532 resetStringInfo(ctx->out);
1533
1534 pq_sendbyte(ctx->out, 'w');
1535 pq_sendint64(ctx->out, lsn); /* dataStart */
1536 pq_sendint64(ctx->out, lsn); /* walEnd */
1537
1538 /*
1539 * Fill out the sendtime later, just as it's done in XLogSendPhysical, but
1540 * reserve space here.
1541 */
1542 pq_sendint64(ctx->out, 0); /* sendtime */
1543}
1544
1545/*
1546 * LogicalDecodingContext 'write' callback.
1547 *
1548 * Actually write out data previously prepared by WalSndPrepareWrite out to
1549 * the network. Take as long as needed, but process replies from the other
1550 * side and check timeouts during that.
1551 */
1552static void
1554 bool last_write)
1555{
1557
1558 /*
1559 * Fill the send timestamp last, so that it is taken as late as possible.
1560 * This is somewhat ugly, but the protocol is set as it's already used for
1561 * several releases by streaming physical replication.
1562 */
1566 memcpy(&ctx->out->data[1 + sizeof(int64) + sizeof(int64)],
1567 tmpbuf.data, sizeof(int64));
1568
1569 /* output previously gathered data in a CopyData packet */
1570 pq_putmessage_noblock('d', ctx->out->data, ctx->out->len);
1571
1573
1574 /* Try to flush pending output to the client */
1575 if (pq_flush_if_writable() != 0)
1577
1578 /* Try taking fast path unless we get too close to walsender timeout. */
1580 wal_sender_timeout / 2) &&
1582 {
1583 return;
1584 }
1585
1586 /* If we have pending write here, go to slow path */
1588}
1589
1590/*
1591 * Wait until there is no pending write. Also process replies from the other
1592 * side and check timeouts during that.
1593 */
1594static void
1596{
1597 for (;;)
1598 {
1599 long sleeptime;
1600
1601 /* Check for input from the client */
1603
1604 /* die if timeout was reached */
1606
1607 /* Send keepalive if the time has come */
1609
1610 if (!pq_is_send_pending())
1611 break;
1612
1614
1615 /* Sleep until something happens or we time out */
1617 WAIT_EVENT_WAL_SENDER_WRITE_DATA);
1618
1619 /* Clear any already-pending wakeups */
1621
1623
1624 /* Process any requests or signals received recently */
1626 {
1627 ConfigReloadPending = false;
1630 }
1631
1632 /* Try to flush pending output to the client */
1633 if (pq_flush_if_writable() != 0)
1635 }
1636
1637 /* reactivate latch so WalSndLoop knows to continue */
1639}
1640
1641/*
1642 * LogicalDecodingContext 'update_progress' callback.
1643 *
1644 * Write the current position to the lag tracker (see XLogSendPhysical).
1645 *
1646 * When skipping empty transactions, send a keepalive message if necessary.
1647 */
1648static void
1650 bool skipped_xact)
1651{
1652 static TimestampTz sendTime = 0;
1654 bool pending_writes = false;
1655 bool end_xact = ctx->end_xact;
1656
1657 /*
1658 * Track lag no more than once per WALSND_LOGICAL_LAG_TRACK_INTERVAL_MS to
1659 * avoid flooding the lag tracker when we commit frequently.
1660 *
1661 * We don't have a mechanism to get the ack for any LSN other than end
1662 * xact LSN from the downstream. So, we track lag only for end of
1663 * transaction LSN.
1664 */
1665#define WALSND_LOGICAL_LAG_TRACK_INTERVAL_MS 1000
1666 if (end_xact && TimestampDifferenceExceeds(sendTime, now,
1668 {
1669 LagTrackerWrite(lsn, now);
1670 sendTime = now;
1671 }
1672
1673 /*
1674 * When skipping empty transactions in synchronous replication, we send a
1675 * keepalive message to avoid delaying such transactions.
1676 *
1677 * It is okay to check sync_standbys_status without lock here as in the
1678 * worst case we will just send an extra keepalive message when it is
1679 * really not required.
1680 */
1681 if (skipped_xact &&
1682 SyncRepRequested() &&
1683 (((volatile WalSndCtlData *) WalSndCtl)->sync_standbys_status & SYNC_STANDBY_DEFINED))
1684 {
1685 WalSndKeepalive(false, lsn);
1686
1687 /* Try to flush pending output to the client */
1688 if (pq_flush_if_writable() != 0)
1690
1691 /* If we have pending write here, make sure it's actually flushed */
1692 if (pq_is_send_pending())
1693 pending_writes = true;
1694 }
1695
1696 /*
1697 * Process pending writes if any or try to send a keepalive if required.
1698 * We don't need to try sending keep alive messages at the transaction end
1699 * as that will be done at a later point in time. This is required only
1700 * for large transactions where we don't send any changes to the
1701 * downstream and the receiver can timeout due to that.
1702 */
1703 if (pending_writes || (!end_xact &&
1705 wal_sender_timeout / 2)))
1707}
1708
1709/*
1710 * Wake up the logical walsender processes with logical failover slots if the
1711 * currently acquired physical slot is specified in synchronized_standby_slots GUC.
1712 */
1713void
1715{
1717
1718 /*
1719 * If we are running in a standby, there is no need to wake up walsenders.
1720 * This is because we do not support syncing slots to cascading standbys,
1721 * so, there are no walsenders waiting for standbys to catch up.
1722 */
1723 if (RecoveryInProgress())
1724 return;
1725
1728}
1729
1730/*
1731 * Returns true if not all standbys have caught up to the flushed position
1732 * (flushed_lsn) when the current acquired slot is a logical failover
1733 * slot and we are streaming; otherwise, returns false.
1734 *
1735 * If returning true, the function sets the appropriate wait event in
1736 * wait_event; otherwise, wait_event is set to 0.
1737 */
1738static bool
1739NeedToWaitForStandbys(XLogRecPtr flushed_lsn, uint32 *wait_event)
1740{
1741 int elevel = got_STOPPING ? ERROR : WARNING;
1742 bool failover_slot;
1743
1744 failover_slot = (replication_active && MyReplicationSlot->data.failover);
1745
1746 /*
1747 * Note that after receiving the shutdown signal, an ERROR is reported if
1748 * any slots are dropped, invalidated, or inactive. This measure is taken
1749 * to prevent the walsender from waiting indefinitely.
1750 */
1751 if (failover_slot && !StandbySlotsHaveCaughtup(flushed_lsn, elevel))
1752 {
1753 *wait_event = WAIT_EVENT_WAIT_FOR_STANDBY_CONFIRMATION;
1754 return true;
1755 }
1756
1757 *wait_event = 0;
1758 return false;
1759}
1760
1761/*
1762 * Returns true if we need to wait for WALs to be flushed to disk, or if not
1763 * all standbys have caught up to the flushed position (flushed_lsn) when the
1764 * current acquired slot is a logical failover slot and we are
1765 * streaming; otherwise, returns false.
1766 *
1767 * If returning true, the function sets the appropriate wait event in
1768 * wait_event; otherwise, wait_event is set to 0.
1769 */
1770static bool
1772 uint32 *wait_event)
1773{
1774 /* Check if we need to wait for WALs to be flushed to disk */
1775 if (target_lsn > flushed_lsn)
1776 {
1777 *wait_event = WAIT_EVENT_WAL_SENDER_WAIT_FOR_WAL;
1778 return true;
1779 }
1780
1781 /* Check if the standby slots have caught up to the flushed position */
1782 return NeedToWaitForStandbys(flushed_lsn, wait_event);
1783}
1784
1785/*
1786 * Wait till WAL < loc is flushed to disk so it can be safely sent to client.
1787 *
1788 * If the walsender holds a logical failover slot, we also wait for all the
1789 * specified streaming replication standby servers to confirm receipt of WAL
1790 * up to RecentFlushPtr. It is beneficial to wait here for the confirmation
1791 * up to RecentFlushPtr rather than waiting before transmitting each change
1792 * to logical subscribers, which is already covered by RecentFlushPtr.
1793 *
1794 * Returns end LSN of flushed WAL. Normally this will be >= loc, but if we
1795 * detect a shutdown request (either from postmaster or client) we will return
1796 * early, so caller must always check.
1797 */
1798static XLogRecPtr
1800{
1801 int wakeEvents;
1802 uint32 wait_event = 0;
1803 static XLogRecPtr RecentFlushPtr = InvalidXLogRecPtr;
1804 TimestampTz last_flush = 0;
1805
1806 /*
1807 * Fast path to avoid acquiring the spinlock in case we already know we
1808 * have enough WAL available and all the standby servers have confirmed
1809 * receipt of WAL up to RecentFlushPtr. This is particularly interesting
1810 * if we're far behind.
1811 */
1812 if (!XLogRecPtrIsInvalid(RecentFlushPtr) &&
1813 !NeedToWaitForWal(loc, RecentFlushPtr, &wait_event))
1814 return RecentFlushPtr;
1815
1816 /*
1817 * Within the loop, we wait for the necessary WALs to be flushed to disk
1818 * first, followed by waiting for standbys to catch up if there are enough
1819 * WALs (see NeedToWaitForWal()) or upon receiving the shutdown signal.
1820 */
1821 for (;;)
1822 {
1823 bool wait_for_standby_at_stop = false;
1824 long sleeptime;
1826
1827 /* Clear any already-pending wakeups */
1829
1831
1832 /* Process any requests or signals received recently */
1834 {
1835 ConfigReloadPending = false;
1838 }
1839
1840 /* Check for input from the client */
1842
1843 /*
1844 * If we're shutting down, trigger pending WAL to be written out,
1845 * otherwise we'd possibly end up waiting for WAL that never gets
1846 * written, because walwriter has shut down already.
1847 */
1848 if (got_STOPPING)
1850
1851 /*
1852 * To avoid the scenario where standbys need to catch up to a newer
1853 * WAL location in each iteration, we update our idea of the currently
1854 * flushed position only if we are not waiting for standbys to catch
1855 * up.
1856 */
1857 if (wait_event != WAIT_EVENT_WAIT_FOR_STANDBY_CONFIRMATION)
1858 {
1859 if (!RecoveryInProgress())
1860 RecentFlushPtr = GetFlushRecPtr(NULL);
1861 else
1862 RecentFlushPtr = GetXLogReplayRecPtr(NULL);
1863 }
1864
1865 /*
1866 * If postmaster asked us to stop and the standby slots have caught up
1867 * to the flushed position, don't wait anymore.
1868 *
1869 * It's important to do this check after the recomputation of
1870 * RecentFlushPtr, so we can send all remaining data before shutting
1871 * down.
1872 */
1873 if (got_STOPPING)
1874 {
1875 if (NeedToWaitForStandbys(RecentFlushPtr, &wait_event))
1876 wait_for_standby_at_stop = true;
1877 else
1878 break;
1879 }
1880
1881 /*
1882 * We only send regular messages to the client for full decoded
1883 * transactions, but a synchronous replication and walsender shutdown
1884 * possibly are waiting for a later location. So, before sleeping, we
1885 * send a ping containing the flush location. If the receiver is
1886 * otherwise idle, this keepalive will trigger a reply. Processing the
1887 * reply will update these MyWalSnd locations.
1888 */
1889 if (MyWalSnd->flush < sentPtr &&
1890 MyWalSnd->write < sentPtr &&
1893
1894 /*
1895 * Exit the loop if already caught up and doesn't need to wait for
1896 * standby slots.
1897 */
1898 if (!wait_for_standby_at_stop &&
1899 !NeedToWaitForWal(loc, RecentFlushPtr, &wait_event))
1900 break;
1901
1902 /*
1903 * Waiting for new WAL or waiting for standbys to catch up. Since we
1904 * need to wait, we're now caught up.
1905 */
1906 WalSndCaughtUp = true;
1907
1908 /*
1909 * Try to flush any pending output to the client.
1910 */
1911 if (pq_flush_if_writable() != 0)
1913
1914 /*
1915 * If we have received CopyDone from the client, sent CopyDone
1916 * ourselves, and the output buffer is empty, it's time to exit
1917 * streaming, so fail the current WAL fetch request.
1918 */
1921 break;
1922
1923 /* die if timeout was reached */
1925
1926 /* Send keepalive if the time has come */
1928
1929 /*
1930 * Sleep until something happens or we time out. Also wait for the
1931 * socket becoming writable, if there's still pending output.
1932 * Otherwise we might sit on sendable output data while waiting for
1933 * new WAL to be generated. (But if we have nothing to send, we don't
1934 * want to wake on socket-writable.)
1935 */
1937 sleeptime = WalSndComputeSleeptime(now);
1938
1939 wakeEvents = WL_SOCKET_READABLE;
1940
1941 if (pq_is_send_pending())
1942 wakeEvents |= WL_SOCKET_WRITEABLE;
1943
1944 Assert(wait_event != 0);
1945
1946 /* Report IO statistics, if needed */
1947 if (TimestampDifferenceExceeds(last_flush, now,
1949 {
1950 pgstat_flush_io(false);
1952 last_flush = now;
1953 }
1954
1955 WalSndWait(wakeEvents, sleeptime, wait_event);
1956 }
1957
1958 /* reactivate latch so WalSndLoop knows to continue */
1960 return RecentFlushPtr;
1961}
1962
1963/*
1964 * Execute an incoming replication command.
1965 *
1966 * Returns true if the cmd_string was recognized as WalSender command, false
1967 * if not.
1968 */
1969bool
1970exec_replication_command(const char *cmd_string)
1971{
1972 yyscan_t scanner;
1973 int parse_rc;
1974 Node *cmd_node;
1975 const char *cmdtag;
1976 MemoryContext old_context = CurrentMemoryContext;
1977
1978 /* We save and re-use the cmd_context across calls */
1979 static MemoryContext cmd_context = NULL;
1980
1981 /*
1982 * If WAL sender has been told that shutdown is getting close, switch its
1983 * status accordingly to handle the next replication commands correctly.
1984 */
1985 if (got_STOPPING)
1987
1988 /*
1989 * Throw error if in stopping mode. We need prevent commands that could
1990 * generate WAL while the shutdown checkpoint is being written. To be
1991 * safe, we just prohibit all new commands.
1992 */
1994 ereport(ERROR,
1995 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1996 errmsg("cannot execute new commands while WAL sender is in stopping mode")));
1997
1998 /*
1999 * CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
2000 * command arrives. Clean up the old stuff if there's anything.
2001 */
2003
2005
2006 /*
2007 * Prepare to parse and execute the command.
2008 *
2009 * Because replication command execution can involve beginning or ending
2010 * transactions, we need a working context that will survive that, so we
2011 * make it a child of TopMemoryContext. That in turn creates a hazard of
2012 * long-lived memory leaks if we lose track of the working context. We
2013 * deal with that by creating it only once per walsender, and resetting it
2014 * for each new command. (Normally this reset is a no-op, but if the
2015 * prior exec_replication_command call failed with an error, it won't be.)
2016 *
2017 * This is subtler than it looks. The transactions we manage can extend
2018 * across replication commands, indeed SnapBuildClearExportedSnapshot
2019 * might have just ended one. Because transaction exit will revert to the
2020 * memory context that was current at transaction start, we need to be
2021 * sure that that context is still valid. That motivates re-using the
2022 * same cmd_context rather than making a new one each time.
2023 */
2024 if (cmd_context == NULL)
2026 "Replication command context",
2028 else
2029 MemoryContextReset(cmd_context);
2030
2031 MemoryContextSwitchTo(cmd_context);
2032
2033 replication_scanner_init(cmd_string, &scanner);
2034
2035 /*
2036 * Is it a WalSender command?
2037 */
2039 {
2040 /* Nope; clean up and get out. */
2042
2043 MemoryContextSwitchTo(old_context);
2044 MemoryContextReset(cmd_context);
2045
2046 /* XXX this is a pretty random place to make this check */
2047 if (MyDatabaseId == InvalidOid)
2048 ereport(ERROR,
2049 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2050 errmsg("cannot execute SQL commands in WAL sender for physical replication")));
2051
2052 /* Tell the caller that this wasn't a WalSender command. */
2053 return false;
2054 }
2055
2056 /*
2057 * Looks like a WalSender command, so parse it.
2058 */
2059 parse_rc = replication_yyparse(&cmd_node, scanner);
2060 if (parse_rc != 0)
2061 ereport(ERROR,
2062 (errcode(ERRCODE_SYNTAX_ERROR),
2063 errmsg_internal("replication command parser returned %d",
2064 parse_rc)));
2066
2067 /*
2068 * Report query to various monitoring facilities. For this purpose, we
2069 * report replication commands just like SQL commands.
2070 */
2071 debug_query_string = cmd_string;
2072
2074
2075 /*
2076 * Log replication command if log_replication_commands is enabled. Even
2077 * when it's disabled, log the command with DEBUG1 level for backward
2078 * compatibility.
2079 */
2081 (errmsg("received replication command: %s", cmd_string)));
2082
2083 /*
2084 * Disallow replication commands in aborted transaction blocks.
2085 */
2087 ereport(ERROR,
2088 (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
2089 errmsg("current transaction is aborted, "
2090 "commands ignored until end of transaction block")));
2091
2093
2094 /*
2095 * Allocate buffers that will be used for each outgoing and incoming
2096 * message. We do this just once per command to reduce palloc overhead.
2097 */
2101
2102 switch (cmd_node->type)
2103 {
2104 case T_IdentifySystemCmd:
2105 cmdtag = "IDENTIFY_SYSTEM";
2106 set_ps_display(cmdtag);
2108 EndReplicationCommand(cmdtag);
2109 break;
2110
2111 case T_ReadReplicationSlotCmd:
2112 cmdtag = "READ_REPLICATION_SLOT";
2113 set_ps_display(cmdtag);
2115 EndReplicationCommand(cmdtag);
2116 break;
2117
2118 case T_BaseBackupCmd:
2119 cmdtag = "BASE_BACKUP";
2120 set_ps_display(cmdtag);
2121 PreventInTransactionBlock(true, cmdtag);
2123 EndReplicationCommand(cmdtag);
2124 break;
2125
2126 case T_CreateReplicationSlotCmd:
2127 cmdtag = "CREATE_REPLICATION_SLOT";
2128 set_ps_display(cmdtag);
2130 EndReplicationCommand(cmdtag);
2131 break;
2132
2133 case T_DropReplicationSlotCmd:
2134 cmdtag = "DROP_REPLICATION_SLOT";
2135 set_ps_display(cmdtag);
2137 EndReplicationCommand(cmdtag);
2138 break;
2139
2140 case T_AlterReplicationSlotCmd:
2141 cmdtag = "ALTER_REPLICATION_SLOT";
2142 set_ps_display(cmdtag);
2144 EndReplicationCommand(cmdtag);
2145 break;
2146
2147 case T_StartReplicationCmd:
2148 {
2149 StartReplicationCmd *cmd = (StartReplicationCmd *) cmd_node;
2150
2151 cmdtag = "START_REPLICATION";
2152 set_ps_display(cmdtag);
2153 PreventInTransactionBlock(true, cmdtag);
2154
2155 if (cmd->kind == REPLICATION_KIND_PHYSICAL)
2156 StartReplication(cmd);
2157 else
2159
2160 /* dupe, but necessary per libpqrcv_endstreaming */
2161 EndReplicationCommand(cmdtag);
2162
2163 Assert(xlogreader != NULL);
2164 break;
2165 }
2166
2167 case T_TimeLineHistoryCmd:
2168 cmdtag = "TIMELINE_HISTORY";
2169 set_ps_display(cmdtag);
2170 PreventInTransactionBlock(true, cmdtag);
2172 EndReplicationCommand(cmdtag);
2173 break;
2174
2175 case T_VariableShowStmt:
2176 {
2178 VariableShowStmt *n = (VariableShowStmt *) cmd_node;
2179
2180 cmdtag = "SHOW";
2181 set_ps_display(cmdtag);
2182
2183 /* syscache access needs a transaction environment */
2185 GetPGVariable(n->name, dest);
2187 EndReplicationCommand(cmdtag);
2188 }
2189 break;
2190
2191 case T_UploadManifestCmd:
2192 cmdtag = "UPLOAD_MANIFEST";
2193 set_ps_display(cmdtag);
2194 PreventInTransactionBlock(true, cmdtag);
2196 EndReplicationCommand(cmdtag);
2197 break;
2198
2199 default:
2200 elog(ERROR, "unrecognized replication command node tag: %u",
2201 cmd_node->type);
2202 }
2203
2204 /*
2205 * Done. Revert to caller's memory context, and clean out the cmd_context
2206 * to recover memory right away.
2207 */
2208 MemoryContextSwitchTo(old_context);
2209 MemoryContextReset(cmd_context);
2210
2211 /*
2212 * We need not update ps display or pg_stat_activity, because PostgresMain
2213 * will reset those to "idle". But we must reset debug_query_string to
2214 * ensure it doesn't become a dangling pointer.
2215 */
2216 debug_query_string = NULL;
2217
2218 return true;
2219}
2220
2221/*
2222 * Process any incoming messages while streaming. Also checks if the remote
2223 * end has closed the connection.
2224 */
2225static void
2227{
2228 unsigned char firstchar;
2229 int maxmsglen;
2230 int r;
2231 bool received = false;
2232
2234
2235 /*
2236 * If we already received a CopyDone from the frontend, any subsequent
2237 * message is the beginning of a new command, and should be processed in
2238 * the main processing loop.
2239 */
2240 while (!streamingDoneReceiving)
2241 {
2243 r = pq_getbyte_if_available(&firstchar);
2244 if (r < 0)
2245 {
2246 /* unexpected error or EOF */
2248 (errcode(ERRCODE_PROTOCOL_VIOLATION),
2249 errmsg("unexpected EOF on standby connection")));
2250 proc_exit(0);
2251 }
2252 if (r == 0)
2253 {
2254 /* no data available without blocking */
2255 pq_endmsgread();
2256 break;
2257 }
2258
2259 /* Validate message type and set packet size limit */
2260 switch (firstchar)
2261 {
2262 case PqMsg_CopyData:
2263 maxmsglen = PQ_LARGE_MESSAGE_LIMIT;
2264 break;
2265 case PqMsg_CopyDone:
2266 case PqMsg_Terminate:
2267 maxmsglen = PQ_SMALL_MESSAGE_LIMIT;
2268 break;
2269 default:
2270 ereport(FATAL,
2271 (errcode(ERRCODE_PROTOCOL_VIOLATION),
2272 errmsg("invalid standby message type \"%c\"",
2273 firstchar)));
2274 maxmsglen = 0; /* keep compiler quiet */
2275 break;
2276 }
2277
2278 /* Read the message contents */
2280 if (pq_getmessage(&reply_message, maxmsglen))
2281 {
2283 (errcode(ERRCODE_PROTOCOL_VIOLATION),
2284 errmsg("unexpected EOF on standby connection")));
2285 proc_exit(0);
2286 }
2287
2288 /* ... and process it */
2289 switch (firstchar)
2290 {
2291 /*
2292 * 'd' means a standby reply wrapped in a CopyData packet.
2293 */
2294 case PqMsg_CopyData:
2296 received = true;
2297 break;
2298
2299 /*
2300 * CopyDone means the standby requested to finish streaming.
2301 * Reply with CopyDone, if we had not sent that already.
2302 */
2303 case PqMsg_CopyDone:
2305 {
2306 pq_putmessage_noblock('c', NULL, 0);
2307 streamingDoneSending = true;
2308 }
2309
2311 received = true;
2312 break;
2313
2314 /*
2315 * 'X' means that the standby is closing down the socket.
2316 */
2317 case PqMsg_Terminate:
2318 proc_exit(0);
2319
2320 default:
2321 Assert(false); /* NOT REACHED */
2322 }
2323 }
2324
2325 /*
2326 * Save the last reply timestamp if we've received at least one reply.
2327 */
2328 if (received)
2329 {
2332 }
2333}
2334
2335/*
2336 * Process a status update message received from standby.
2337 */
2338static void
2340{
2341 char msgtype;
2342
2343 /*
2344 * Check message type from the first byte.
2345 */
2346 msgtype = pq_getmsgbyte(&reply_message);
2347
2348 switch (msgtype)
2349 {
2350 case 'r':
2352 break;
2353
2354 case 'h':
2356 break;
2357
2358 default:
2360 (errcode(ERRCODE_PROTOCOL_VIOLATION),
2361 errmsg("unexpected message type \"%c\"", msgtype)));
2362 proc_exit(0);
2363 }
2364}
2365
2366/*
2367 * Remember that a walreceiver just confirmed receipt of lsn `lsn`.
2368 */
2369static void
2371{
2372 bool changed = false;
2374
2375 Assert(lsn != InvalidXLogRecPtr);
2376 SpinLockAcquire(&slot->mutex);
2377 if (slot->data.restart_lsn != lsn)
2378 {
2379 changed = true;
2380 slot->data.restart_lsn = lsn;
2381 }
2382 SpinLockRelease(&slot->mutex);
2383
2384 if (changed)
2385 {
2389 }
2390
2391 /*
2392 * One could argue that the slot should be saved to disk now, but that'd
2393 * be energy wasted - the worst thing lost information could cause here is
2394 * to give wrong information in a statistics view - we'll just potentially
2395 * be more conservative in removing files.
2396 */
2397}
2398
2399/*
2400 * Regular reply from standby advising of WAL locations on standby server.
2401 */
2402static void
2404{
2405 XLogRecPtr writePtr,
2406 flushPtr,
2407 applyPtr;
2408 bool replyRequested;
2409 TimeOffset writeLag,
2410 flushLag,
2411 applyLag;
2412 bool clearLagTimes;
2414 TimestampTz replyTime;
2415
2416 static bool fullyAppliedLastTime = false;
2417
2418 /* the caller already consumed the msgtype byte */
2419 writePtr = pq_getmsgint64(&reply_message);
2420 flushPtr = pq_getmsgint64(&reply_message);
2421 applyPtr = pq_getmsgint64(&reply_message);
2422 replyTime = pq_getmsgint64(&reply_message);
2423 replyRequested = pq_getmsgbyte(&reply_message);
2424
2426 {
2427 char *replyTimeStr;
2428
2429 /* Copy because timestamptz_to_str returns a static buffer */
2430 replyTimeStr = pstrdup(timestamptz_to_str(replyTime));
2431
2432 elog(DEBUG2, "write %X/%X flush %X/%X apply %X/%X%s reply_time %s",
2433 LSN_FORMAT_ARGS(writePtr),
2434 LSN_FORMAT_ARGS(flushPtr),
2435 LSN_FORMAT_ARGS(applyPtr),
2436 replyRequested ? " (reply requested)" : "",
2437 replyTimeStr);
2438
2439 pfree(replyTimeStr);
2440 }
2441
2442 /* See if we can compute the round-trip lag for these positions. */
2444 writeLag = LagTrackerRead(SYNC_REP_WAIT_WRITE, writePtr, now);
2445 flushLag = LagTrackerRead(SYNC_REP_WAIT_FLUSH, flushPtr, now);
2446 applyLag = LagTrackerRead(SYNC_REP_WAIT_APPLY, applyPtr, now);
2447
2448 /*
2449 * If the standby reports that it has fully replayed the WAL in two
2450 * consecutive reply messages, then the second such message must result
2451 * from wal_receiver_status_interval expiring on the standby. This is a
2452 * convenient time to forget the lag times measured when it last
2453 * wrote/flushed/applied a WAL record, to avoid displaying stale lag data
2454 * until more WAL traffic arrives.
2455 */
2456 clearLagTimes = false;
2457 if (applyPtr == sentPtr)
2458 {
2459 if (fullyAppliedLastTime)
2460 clearLagTimes = true;
2461 fullyAppliedLastTime = true;
2462 }
2463 else
2464 fullyAppliedLastTime = false;
2465
2466 /* Send a reply if the standby requested one. */
2467 if (replyRequested)
2469
2470 /*
2471 * Update shared state for this WalSender process based on reply data from
2472 * standby.
2473 */
2474 {
2475 WalSnd *walsnd = MyWalSnd;
2476
2477 SpinLockAcquire(&walsnd->mutex);
2478 walsnd->write = writePtr;
2479 walsnd->flush = flushPtr;
2480 walsnd->apply = applyPtr;
2481 if (writeLag != -1 || clearLagTimes)
2482 walsnd->writeLag = writeLag;
2483 if (flushLag != -1 || clearLagTimes)
2484 walsnd->flushLag = flushLag;
2485 if (applyLag != -1 || clearLagTimes)
2486 walsnd->applyLag = applyLag;
2487 walsnd->replyTime = replyTime;
2488 SpinLockRelease(&walsnd->mutex);
2489 }
2490
2493
2494 /*
2495 * Advance our local xmin horizon when the client confirmed a flush.
2496 */
2497 if (MyReplicationSlot && flushPtr != InvalidXLogRecPtr)
2498 {
2501 else
2503 }
2504}
2505
2506/* compute new replication slot xmin horizon if needed */
2507static void
2509{
2510 bool changed = false;
2512
2513 SpinLockAcquire(&slot->mutex);
2515
2516 /*
2517 * For physical replication we don't need the interlock provided by xmin
2518 * and effective_xmin since the consequences of a missed increase are
2519 * limited to query cancellations, so set both at once.
2520 */
2521 if (!TransactionIdIsNormal(slot->data.xmin) ||
2522 !TransactionIdIsNormal(feedbackXmin) ||
2523 TransactionIdPrecedes(slot->data.xmin, feedbackXmin))
2524 {
2525 changed = true;
2526 slot->data.xmin = feedbackXmin;
2527 slot->effective_xmin = feedbackXmin;
2528 }
2530 !TransactionIdIsNormal(feedbackCatalogXmin) ||
2531 TransactionIdPrecedes(slot->data.catalog_xmin, feedbackCatalogXmin))
2532 {
2533 changed = true;
2534 slot->data.catalog_xmin = feedbackCatalogXmin;
2535 slot->effective_catalog_xmin = feedbackCatalogXmin;
2536 }
2537 SpinLockRelease(&slot->mutex);
2538
2539 if (changed)
2540 {
2543 }
2544}
2545
2546/*
2547 * Check that the provided xmin/epoch are sane, that is, not in the future
2548 * and not so far back as to be already wrapped around.
2549 *
2550 * Epoch of nextXid should be same as standby, or if the counter has
2551 * wrapped, then one greater than standby.
2552 *
2553 * This check doesn't care about whether clog exists for these xids
2554 * at all.
2555 */
2556static bool
2558{
2559 FullTransactionId nextFullXid;
2560 TransactionId nextXid;
2561 uint32 nextEpoch;
2562
2563 nextFullXid = ReadNextFullTransactionId();
2564 nextXid = XidFromFullTransactionId(nextFullXid);
2565 nextEpoch = EpochFromFullTransactionId(nextFullXid);
2566
2567 if (xid <= nextXid)
2568 {
2569 if (epoch != nextEpoch)
2570 return false;
2571 }
2572 else
2573 {
2574 if (epoch + 1 != nextEpoch)
2575 return false;
2576 }
2577
2578 if (!TransactionIdPrecedesOrEquals(xid, nextXid))
2579 return false; /* epoch OK, but it's wrapped around */
2580
2581 return true;
2582}
2583
2584/*
2585 * Hot Standby feedback
2586 */
2587static void
2589{
2590 TransactionId feedbackXmin;
2591 uint32 feedbackEpoch;
2592 TransactionId feedbackCatalogXmin;
2593 uint32 feedbackCatalogEpoch;
2594 TimestampTz replyTime;
2595
2596 /*
2597 * Decipher the reply message. The caller already consumed the msgtype
2598 * byte. See XLogWalRcvSendHSFeedback() in walreceiver.c for the creation
2599 * of this message.
2600 */
2601 replyTime = pq_getmsgint64(&reply_message);
2602 feedbackXmin = pq_getmsgint(&reply_message, 4);
2603 feedbackEpoch = pq_getmsgint(&reply_message, 4);
2604 feedbackCatalogXmin = pq_getmsgint(&reply_message, 4);
2605 feedbackCatalogEpoch = pq_getmsgint(&reply_message, 4);
2606
2608 {
2609 char *replyTimeStr;
2610
2611 /* Copy because timestamptz_to_str returns a static buffer */
2612 replyTimeStr = pstrdup(timestamptz_to_str(replyTime));
2613
2614 elog(DEBUG2, "hot standby feedback xmin %u epoch %u, catalog_xmin %u epoch %u reply_time %s",
2615 feedbackXmin,
2616 feedbackEpoch,
2617 feedbackCatalogXmin,
2618 feedbackCatalogEpoch,
2619 replyTimeStr);
2620
2621 pfree(replyTimeStr);
2622 }
2623
2624 /*
2625 * Update shared state for this WalSender process based on reply data from
2626 * standby.
2627 */
2628 {
2629 WalSnd *walsnd = MyWalSnd;
2630
2631 SpinLockAcquire(&walsnd->mutex);
2632 walsnd->replyTime = replyTime;
2633 SpinLockRelease(&walsnd->mutex);
2634 }
2635
2636 /*
2637 * Unset WalSender's xmins if the feedback message values are invalid.
2638 * This happens when the downstream turned hot_standby_feedback off.
2639 */
2640 if (!TransactionIdIsNormal(feedbackXmin)
2641 && !TransactionIdIsNormal(feedbackCatalogXmin))
2642 {
2644 if (MyReplicationSlot != NULL)
2645 PhysicalReplicationSlotNewXmin(feedbackXmin, feedbackCatalogXmin);
2646 return;
2647 }
2648
2649 /*
2650 * Check that the provided xmin/epoch are sane, that is, not in the future
2651 * and not so far back as to be already wrapped around. Ignore if not.
2652 */
2653 if (TransactionIdIsNormal(feedbackXmin) &&
2654 !TransactionIdInRecentPast(feedbackXmin, feedbackEpoch))
2655 return;
2656
2657 if (TransactionIdIsNormal(feedbackCatalogXmin) &&
2658 !TransactionIdInRecentPast(feedbackCatalogXmin, feedbackCatalogEpoch))
2659 return;
2660
2661 /*
2662 * Set the WalSender's xmin equal to the standby's requested xmin, so that
2663 * the xmin will be taken into account by GetSnapshotData() /
2664 * ComputeXidHorizons(). This will hold back the removal of dead rows and
2665 * thereby prevent the generation of cleanup conflicts on the standby
2666 * server.
2667 *
2668 * There is a small window for a race condition here: although we just
2669 * checked that feedbackXmin precedes nextXid, the nextXid could have
2670 * gotten advanced between our fetching it and applying the xmin below,
2671 * perhaps far enough to make feedbackXmin wrap around. In that case the
2672 * xmin we set here would be "in the future" and have no effect. No point
2673 * in worrying about this since it's too late to save the desired data
2674 * anyway. Assuming that the standby sends us an increasing sequence of
2675 * xmins, this could only happen during the first reply cycle, else our
2676 * own xmin would prevent nextXid from advancing so far.
2677 *
2678 * We don't bother taking the ProcArrayLock here. Setting the xmin field
2679 * is assumed atomic, and there's no real need to prevent concurrent
2680 * horizon determinations. (If we're moving our xmin forward, this is
2681 * obviously safe, and if we're moving it backwards, well, the data is at
2682 * risk already since a VACUUM could already have determined the horizon.)
2683 *
2684 * If we're using a replication slot we reserve the xmin via that,
2685 * otherwise via the walsender's PGPROC entry. We can only track the
2686 * catalog xmin separately when using a slot, so we store the least of the
2687 * two provided when not using a slot.
2688 *
2689 * XXX: It might make sense to generalize the ephemeral slot concept and
2690 * always use the slot mechanism to handle the feedback xmin.
2691 */
2692 if (MyReplicationSlot != NULL) /* XXX: persistency configurable? */
2693 PhysicalReplicationSlotNewXmin(feedbackXmin, feedbackCatalogXmin);
2694 else
2695 {
2696 if (TransactionIdIsNormal(feedbackCatalogXmin)
2697 && TransactionIdPrecedes(feedbackCatalogXmin, feedbackXmin))
2698 MyProc->xmin = feedbackCatalogXmin;
2699 else
2700 MyProc->xmin = feedbackXmin;
2701 }
2702}
2703
2704/*
2705 * Compute how long send/receive loops should sleep.
2706 *
2707 * If wal_sender_timeout is enabled we want to wake up in time to send
2708 * keepalives and to abort the connection if wal_sender_timeout has been
2709 * reached.
2710 */
2711static long
2713{
2714 long sleeptime = 10000; /* 10 s */
2715
2717 {
2718 TimestampTz wakeup_time;
2719
2720 /*
2721 * At the latest stop sleeping once wal_sender_timeout has been
2722 * reached.
2723 */
2726
2727 /*
2728 * If no ping has been sent yet, wakeup when it's time to do so.
2729 * WalSndKeepaliveIfNecessary() wants to send a keepalive once half of
2730 * the timeout passed without a response.
2731 */
2734 wal_sender_timeout / 2);
2735
2736 /* Compute relative time until wakeup. */
2737 sleeptime = TimestampDifferenceMilliseconds(now, wakeup_time);
2738 }
2739
2740 return sleeptime;
2741}
2742
2743/*
2744 * Check whether there have been responses by the client within
2745 * wal_sender_timeout and shutdown if not. Using last_processing as the
2746 * reference point avoids counting server-side stalls against the client.
2747 * However, a long server-side stall can make WalSndKeepaliveIfNecessary()
2748 * postdate last_processing by more than wal_sender_timeout. If that happens,
2749 * the client must reply almost immediately to avoid a timeout. This rarely
2750 * affects the default configuration, under which clients spontaneously send a
2751 * message every standby_message_timeout = wal_sender_timeout/6 = 10s. We
2752 * could eliminate that problem by recognizing timeout expiration at
2753 * wal_sender_timeout/2 after the keepalive.
2754 */
2755static void
2757{
2758 TimestampTz timeout;
2759
2760 /* don't bail out if we're doing something that doesn't require timeouts */
2761 if (last_reply_timestamp <= 0)
2762 return;
2763
2766
2767 if (wal_sender_timeout > 0 && last_processing >= timeout)
2768 {
2769 /*
2770 * Since typically expiration of replication timeout means
2771 * communication problem, we don't send the error message to the
2772 * standby.
2773 */
2775 (errmsg("terminating walsender process due to replication timeout")));
2776
2778 }
2779}
2780
2781/* Main loop of walsender process that streams the WAL over Copy messages. */
2782static void
2784{
2785 TimestampTz last_flush = 0;
2786
2787 /*
2788 * Initialize the last reply timestamp. That enables timeout processing
2789 * from hereon.
2790 */
2793
2794 /*
2795 * Loop until we reach the end of this timeline or the client requests to
2796 * stop streaming.
2797 */
2798 for (;;)
2799 {
2800 /* Clear any already-pending wakeups */
2802
2804
2805 /* Process any requests or signals received recently */
2807 {
2808 ConfigReloadPending = false;
2811 }
2812
2813 /* Check for input from the client */
2815
2816 /*
2817 * If we have received CopyDone from the client, sent CopyDone
2818 * ourselves, and the output buffer is empty, it's time to exit
2819 * streaming.
2820 */
2823 break;
2824
2825 /*
2826 * If we don't have any pending data in the output buffer, try to send
2827 * some more. If there is some, we don't bother to call send_data
2828 * again until we've flushed it ... but we'd better assume we are not
2829 * caught up.
2830 */
2831 if (!pq_is_send_pending())
2832 send_data();
2833 else
2834 WalSndCaughtUp = false;
2835
2836 /* Try to flush pending output to the client */
2837 if (pq_flush_if_writable() != 0)
2839
2840 /* If nothing remains to be sent right now ... */
2842 {
2843 /*
2844 * If we're in catchup state, move to streaming. This is an
2845 * important state change for users to know about, since before
2846 * this point data loss might occur if the primary dies and we
2847 * need to failover to the standby. The state change is also
2848 * important for synchronous replication, since commits that
2849 * started to wait at that point might wait for some time.
2850 */
2852 {
2854 (errmsg_internal("\"%s\" has now caught up with upstream server",
2857 }
2858
2859 /*
2860 * When SIGUSR2 arrives, we send any outstanding logs up to the
2861 * shutdown checkpoint record (i.e., the latest record), wait for
2862 * them to be replicated to the standby, and exit. This may be a
2863 * normal termination at shutdown, or a promotion, the walsender
2864 * is not sure which.
2865 */
2866 if (got_SIGUSR2)
2867 WalSndDone(send_data);
2868 }
2869
2870 /* Check for replication timeout. */
2872
2873 /* Send keepalive if the time has come */
2875
2876 /*
2877 * Block if we have unsent data. XXX For logical replication, let
2878 * WalSndWaitForWal() handle any other blocking; idle receivers need
2879 * its additional actions. For physical replication, also block if
2880 * caught up; its send_data does not block.
2881 *
2882 * The IO statistics are reported in WalSndWaitForWal() for the
2883 * logical WAL senders.
2884 */
2885 if ((WalSndCaughtUp && send_data != XLogSendLogical &&
2888 {
2889 long sleeptime;
2890 int wakeEvents;
2892
2894 wakeEvents = WL_SOCKET_READABLE;
2895 else
2896 wakeEvents = 0;
2897
2898 /*
2899 * Use fresh timestamp, not last_processing, to reduce the chance
2900 * of reaching wal_sender_timeout before sending a keepalive.
2901 */
2903 sleeptime = WalSndComputeSleeptime(now);
2904
2905 if (pq_is_send_pending())
2906 wakeEvents |= WL_SOCKET_WRITEABLE;
2907
2908 /* Report IO statistics, if needed */
2909 if (TimestampDifferenceExceeds(last_flush, now,
2911 {
2912 pgstat_flush_io(false);
2914 last_flush = now;
2915 }
2916
2917 /* Sleep until something happens or we time out */
2918 WalSndWait(wakeEvents, sleeptime, WAIT_EVENT_WAL_SENDER_MAIN);
2919 }
2920 }
2921}
2922
2923/* Initialize a per-walsender data structure for this walsender process */
2924static void
2926{
2927 int i;
2928
2929 /*
2930 * WalSndCtl should be set up already (we inherit this by fork() or
2931 * EXEC_BACKEND mechanism from the postmaster).
2932 */
2933 Assert(WalSndCtl != NULL);
2934 Assert(MyWalSnd == NULL);
2935
2936 /*
2937 * Find a free walsender slot and reserve it. This must not fail due to
2938 * the prior check for free WAL senders in InitProcess().
2939 */
2940 for (i = 0; i < max_wal_senders; i++)
2941 {
2942 WalSnd *walsnd = &WalSndCtl->walsnds[i];
2943
2944 SpinLockAcquire(&walsnd->mutex);
2945
2946 if (walsnd->pid != 0)
2947 {
2948 SpinLockRelease(&walsnd->mutex);
2949 continue;
2950 }
2951 else
2952 {
2953 /*
2954 * Found a free slot. Reserve it for us.
2955 */
2956 walsnd->pid = MyProcPid;
2957 walsnd->state = WALSNDSTATE_STARTUP;
2958 walsnd->sentPtr = InvalidXLogRecPtr;
2959 walsnd->needreload = false;
2960 walsnd->write = InvalidXLogRecPtr;
2961 walsnd->flush = InvalidXLogRecPtr;
2962 walsnd->apply = InvalidXLogRecPtr;
2963 walsnd->writeLag = -1;
2964 walsnd->flushLag = -1;
2965 walsnd->applyLag = -1;
2966 walsnd->sync_standby_priority = 0;
2967 walsnd->replyTime = 0;
2968
2969 /*
2970 * The kind assignment is done here and not in StartReplication()
2971 * and StartLogicalReplication(). Indeed, the logical walsender
2972 * needs to read WAL records (like snapshot of running
2973 * transactions) during the slot creation. So it needs to be woken
2974 * up based on its kind.
2975 *
2976 * The kind assignment could also be done in StartReplication(),
2977 * StartLogicalReplication() and CREATE_REPLICATION_SLOT but it
2978 * seems better to set it on one place.
2979 */
2980 if (MyDatabaseId == InvalidOid)
2982 else
2984
2985 SpinLockRelease(&walsnd->mutex);
2986 /* don't need the lock anymore */
2987 MyWalSnd = (WalSnd *) walsnd;
2988
2989 break;
2990 }
2991 }
2992
2993 Assert(MyWalSnd != NULL);
2994
2995 /* Arrange to clean up at walsender exit */
2997}
2998
2999/* Destroy the per-walsender data structure for this walsender process */
3000static void
3002{
3003 WalSnd *walsnd = MyWalSnd;
3004
3005 Assert(walsnd != NULL);
3006
3007 MyWalSnd = NULL;
3008
3009 SpinLockAcquire(&walsnd->mutex);
3010 /* Mark WalSnd struct as no longer being in use. */
3011 walsnd->pid = 0;
3012 SpinLockRelease(&walsnd->mutex);
3013}
3014
3015/* XLogReaderRoutine->segment_open callback */
3016static void
3018 TimeLineID *tli_p)
3019{
3020 char path[MAXPGPATH];
3021
3022 /*-------
3023 * When reading from a historic timeline, and there is a timeline switch
3024 * within this segment, read from the WAL segment belonging to the new
3025 * timeline.
3026 *
3027 * For example, imagine that this server is currently on timeline 5, and
3028 * we're streaming timeline 4. The switch from timeline 4 to 5 happened at
3029 * 0/13002088. In pg_wal, we have these files:
3030 *
3031 * ...
3032 * 000000040000000000000012
3033 * 000000040000000000000013
3034 * 000000050000000000000013
3035 * 000000050000000000000014
3036 * ...
3037 *
3038 * In this situation, when requested to send the WAL from segment 0x13, on
3039 * timeline 4, we read the WAL from file 000000050000000000000013. Archive
3040 * recovery prefers files from newer timelines, so if the segment was
3041 * restored from the archive on this server, the file belonging to the old
3042 * timeline, 000000040000000000000013, might not exist. Their contents are
3043 * equal up to the switchpoint, because at a timeline switch, the used
3044 * portion of the old segment is copied to the new file.
3045 */
3046 *tli_p = sendTimeLine;
3048 {
3049 XLogSegNo endSegNo;
3050
3051 XLByteToSeg(sendTimeLineValidUpto, endSegNo, state->segcxt.ws_segsize);
3052 if (nextSegNo == endSegNo)
3053 *tli_p = sendTimeLineNextTLI;
3054 }
3055
3056 XLogFilePath(path, *tli_p, nextSegNo, state->segcxt.ws_segsize);
3057 state->seg.ws_file = BasicOpenFile(path, O_RDONLY | PG_BINARY);
3058 if (state->seg.ws_file >= 0)
3059 return;
3060
3061 /*
3062 * If the file is not found, assume it's because the standby asked for a
3063 * too old WAL segment that has already been removed or recycled.
3064 */
3065 if (errno == ENOENT)
3066 {
3067 char xlogfname[MAXFNAMELEN];
3068 int save_errno = errno;
3069
3070 XLogFileName(xlogfname, *tli_p, nextSegNo, wal_segment_size);
3071 errno = save_errno;
3072 ereport(ERROR,
3074 errmsg("requested WAL segment %s has already been removed",
3075 xlogfname)));
3076 }
3077 else
3078 ereport(ERROR,
3080 errmsg("could not open file \"%s\": %m",
3081 path)));
3082}
3083
3084/*
3085 * Send out the WAL in its normal physical/stored form.
3086 *
3087 * Read up to MAX_SEND_SIZE bytes of WAL that's been flushed to disk,
3088 * but not yet sent to the client, and buffer it in the libpq output
3089 * buffer.
3090 *
3091 * If there is no unsent WAL remaining, WalSndCaughtUp is set to true,
3092 * otherwise WalSndCaughtUp is set to false.
3093 */
3094static void
3096{
3097 XLogRecPtr SendRqstPtr;
3098 XLogRecPtr startptr;
3099 XLogRecPtr endptr;
3100 Size nbytes;
3101 XLogSegNo segno;
3102 WALReadError errinfo;
3103 Size rbytes;
3104
3105 /* If requested switch the WAL sender to the stopping state. */
3106 if (got_STOPPING)
3108
3110 {
3111 WalSndCaughtUp = true;
3112 return;
3113 }
3114
3115 /* Figure out how far we can safely send the WAL. */
3117 {
3118 /*
3119 * Streaming an old timeline that's in this server's history, but is
3120 * not the one we're currently inserting or replaying. It can be
3121 * streamed up to the point where we switched off that timeline.
3122 */
3123 SendRqstPtr = sendTimeLineValidUpto;
3124 }
3125 else if (am_cascading_walsender)
3126 {
3127 TimeLineID SendRqstTLI;
3128
3129 /*
3130 * Streaming the latest timeline on a standby.
3131 *
3132 * Attempt to send all WAL that has already been replayed, so that we
3133 * know it's valid. If we're receiving WAL through streaming
3134 * replication, it's also OK to send any WAL that has been received
3135 * but not replayed.
3136 *
3137 * The timeline we're recovering from can change, or we can be
3138 * promoted. In either case, the current timeline becomes historic. We
3139 * need to detect that so that we don't try to stream past the point
3140 * where we switched to another timeline. We check for promotion or
3141 * timeline switch after calculating FlushPtr, to avoid a race
3142 * condition: if the timeline becomes historic just after we checked
3143 * that it was still current, it's still be OK to stream it up to the
3144 * FlushPtr that was calculated before it became historic.
3145 */
3146 bool becameHistoric = false;
3147
3148 SendRqstPtr = GetStandbyFlushRecPtr(&SendRqstTLI);
3149
3150 if (!RecoveryInProgress())
3151 {
3152 /* We have been promoted. */
3153 SendRqstTLI = GetWALInsertionTimeLine();
3154 am_cascading_walsender = false;
3155 becameHistoric = true;
3156 }
3157 else
3158 {
3159 /*
3160 * Still a cascading standby. But is the timeline we're sending
3161 * still the one recovery is recovering from?
3162 */
3163 if (sendTimeLine != SendRqstTLI)
3164 becameHistoric = true;
3165 }
3166
3167 if (becameHistoric)
3168 {
3169 /*
3170 * The timeline we were sending has become historic. Read the
3171 * timeline history file of the new timeline to see where exactly
3172 * we forked off from the timeline we were sending.
3173 */
3174 List *history;
3175
3176 history = readTimeLineHistory(SendRqstTLI);
3178
3180 list_free_deep(history);
3181
3183
3184 SendRqstPtr = sendTimeLineValidUpto;
3185 }
3186 }
3187 else
3188 {
3189 /*
3190 * Streaming the current timeline on a primary.
3191 *
3192 * Attempt to send all data that's already been written out and
3193 * fsync'd to disk. We cannot go further than what's been written out
3194 * given the current implementation of WALRead(). And in any case
3195 * it's unsafe to send WAL that is not securely down to disk on the
3196 * primary: if the primary subsequently crashes and restarts, standbys
3197 * must not have applied any WAL that got lost on the primary.
3198 */
3199 SendRqstPtr = GetFlushRecPtr(NULL);
3200 }
3201
3202 /*
3203 * Record the current system time as an approximation of the time at which
3204 * this WAL location was written for the purposes of lag tracking.
3205 *
3206 * In theory we could make XLogFlush() record a time in shmem whenever WAL
3207 * is flushed and we could get that time as well as the LSN when we call
3208 * GetFlushRecPtr() above (and likewise for the cascading standby
3209 * equivalent), but rather than putting any new code into the hot WAL path
3210 * it seems good enough to capture the time here. We should reach this
3211 * after XLogFlush() runs WalSndWakeupProcessRequests(), and although that
3212 * may take some time, we read the WAL flush pointer and take the time
3213 * very close to together here so that we'll get a later position if it is
3214 * still moving.
3215 *
3216 * Because LagTrackerWrite ignores samples when the LSN hasn't advanced,
3217 * this gives us a cheap approximation for the WAL flush time for this
3218 * LSN.
3219 *
3220 * Note that the LSN is not necessarily the LSN for the data contained in
3221 * the present message; it's the end of the WAL, which might be further
3222 * ahead. All the lag tracking machinery cares about is finding out when
3223 * that arbitrary LSN is eventually reported as written, flushed and
3224 * applied, so that it can measure the elapsed time.
3225 */
3226 LagTrackerWrite(SendRqstPtr, GetCurrentTimestamp());
3227
3228 /*
3229 * If this is a historic timeline and we've reached the point where we
3230 * forked to the next timeline, stop streaming.
3231 *
3232 * Note: We might already have sent WAL > sendTimeLineValidUpto. The
3233 * startup process will normally replay all WAL that has been received
3234 * from the primary, before promoting, but if the WAL streaming is
3235 * terminated at a WAL page boundary, the valid portion of the timeline
3236 * might end in the middle of a WAL record. We might've already sent the
3237 * first half of that partial WAL record to the cascading standby, so that
3238 * sentPtr > sendTimeLineValidUpto. That's OK; the cascading standby can't
3239 * replay the partial WAL record either, so it can still follow our
3240 * timeline switch.
3241 */
3243 {
3244 /* close the current file. */
3245 if (xlogreader->seg.ws_file >= 0)
3247
3248 /* Send CopyDone */
3249 pq_putmessage_noblock('c', NULL, 0);
3250 streamingDoneSending = true;
3251
3252 WalSndCaughtUp = true;
3253
3254 elog(DEBUG1, "walsender reached end of timeline at %X/%X (sent up to %X/%X)",
3257 return;
3258 }
3259
3260 /* Do we have any work to do? */
3261 Assert(sentPtr <= SendRqstPtr);
3262 if (SendRqstPtr <= sentPtr)
3263 {
3264 WalSndCaughtUp = true;
3265 return;
3266 }
3267
3268 /*
3269 * Figure out how much to send in one message. If there's no more than
3270 * MAX_SEND_SIZE bytes to send, send everything. Otherwise send
3271 * MAX_SEND_SIZE bytes, but round back to logfile or page boundary.
3272 *
3273 * The rounding is not only for performance reasons. Walreceiver relies on
3274 * the fact that we never split a WAL record across two messages. Since a
3275 * long WAL record is split at page boundary into continuation records,
3276 * page boundary is always a safe cut-off point. We also assume that
3277 * SendRqstPtr never points to the middle of a WAL record.
3278 */
3279 startptr = sentPtr;
3280 endptr = startptr;
3281 endptr += MAX_SEND_SIZE;
3282
3283 /* if we went beyond SendRqstPtr, back off */
3284 if (SendRqstPtr <= endptr)
3285 {
3286 endptr = SendRqstPtr;
3288 WalSndCaughtUp = false;
3289 else
3290 WalSndCaughtUp = true;
3291 }
3292 else
3293 {
3294 /* round down to page boundary. */
3295 endptr -= (endptr % XLOG_BLCKSZ);
3296 WalSndCaughtUp = false;
3297 }
3298
3299 nbytes = endptr - startptr;
3300 Assert(nbytes <= MAX_SEND_SIZE);
3301
3302 /*
3303 * OK to read and send the slice.
3304 */
3307
3308 pq_sendint64(&output_message, startptr); /* dataStart */
3309 pq_sendint64(&output_message, SendRqstPtr); /* walEnd */
3310 pq_sendint64(&output_message, 0); /* sendtime, filled in last */
3311
3312 /*
3313 * Read the log directly into the output buffer to avoid extra memcpy
3314 * calls.
3315 */
3317
3318retry:
3319 /* attempt to read WAL from WAL buffers first */
3321 startptr, nbytes, xlogreader->seg.ws_tli);
3322 output_message.len += rbytes;
3323 startptr += rbytes;
3324 nbytes -= rbytes;
3325
3326 /* now read the remaining WAL from WAL file */
3327 if (nbytes > 0 &&
3330 startptr,
3331 nbytes,
3332 xlogreader->seg.ws_tli, /* Pass the current TLI because
3333 * only WalSndSegmentOpen controls
3334 * whether new TLI is needed. */
3335 &errinfo))
3336 WALReadRaiseError(&errinfo);
3337
3338 /* See logical_read_xlog_page(). */
3339 XLByteToSeg(startptr, segno, xlogreader->segcxt.ws_segsize);
3341
3342 /*
3343 * During recovery, the currently-open WAL file might be replaced with the
3344 * file of the same name retrieved from archive. So we always need to
3345 * check what we read was valid after reading into the buffer. If it's
3346 * invalid, we try to open and read the file again.
3347 */
3349 {
3350 WalSnd *walsnd = MyWalSnd;
3351 bool reload;
3352
3353 SpinLockAcquire(&walsnd->mutex);
3354 reload = walsnd->needreload;
3355 walsnd->needreload = false;
3356 SpinLockRelease(&walsnd->mutex);
3357
3358 if (reload && xlogreader->seg.ws_file >= 0)
3359 {
3361
3362 goto retry;
3363 }
3364 }
3365
3366 output_message.len += nbytes;
3368
3369 /*
3370 * Fill the send timestamp last, so that it is taken as late as possible.
3371 */
3374 memcpy(&output_message.data[1 + sizeof(int64) + sizeof(int64)],
3375 tmpbuf.data, sizeof(int64));
3376
3378
3379 sentPtr = endptr;
3380
3381 /* Update shared memory status */
3382 {
3383 WalSnd *walsnd = MyWalSnd;
3384
3385 SpinLockAcquire(&walsnd->mutex);
3386 walsnd->sentPtr = sentPtr;
3387 SpinLockRelease(&walsnd->mutex);
3388 }
3389
3390 /* Report progress of XLOG streaming in PS display */
3392 {
3393 char activitymsg[50];
3394
3395 snprintf(activitymsg, sizeof(activitymsg), "streaming %X/%X",
3397 set_ps_display(activitymsg);
3398 }
3399}
3400
3401/*
3402 * Stream out logically decoded data.
3403 */
3404static void
3406{
3407 XLogRecord *record;
3408 char *errm;
3409
3410 /*
3411 * We'll use the current flush point to determine whether we've caught up.
3412 * This variable is static in order to cache it across calls. Caching is
3413 * helpful because GetFlushRecPtr() needs to acquire a heavily-contended
3414 * spinlock.
3415 */
3416 static XLogRecPtr flushPtr = InvalidXLogRecPtr;
3417
3418 /*
3419 * Don't know whether we've caught up yet. We'll set WalSndCaughtUp to
3420 * true in WalSndWaitForWal, if we're actually waiting. We also set to
3421 * true if XLogReadRecord() had to stop reading but WalSndWaitForWal
3422 * didn't wait - i.e. when we're shutting down.
3423 */
3424 WalSndCaughtUp = false;
3425
3426 record = XLogReadRecord(logical_decoding_ctx->reader, &errm);
3427
3428 /* xlog record was invalid */
3429 if (errm != NULL)
3430 elog(ERROR, "could not find record while sending logically-decoded data: %s",
3431 errm);
3432
3433 if (record != NULL)
3434 {
3435 /*
3436 * Note the lack of any call to LagTrackerWrite() which is handled by
3437 * WalSndUpdateProgress which is called by output plugin through
3438 * logical decoding write api.
3439 */
3441
3443 }
3444
3445 /*
3446 * If first time through in this session, initialize flushPtr. Otherwise,
3447 * we only need to update flushPtr if EndRecPtr is past it.
3448 */
3449 if (flushPtr == InvalidXLogRecPtr ||
3451 {
3453 flushPtr = GetStandbyFlushRecPtr(NULL);
3454 else
3455 flushPtr = GetFlushRecPtr(NULL);
3456 }
3457
3458 /* If EndRecPtr is still past our flushPtr, it means we caught up. */
3459 if (logical_decoding_ctx->reader->EndRecPtr >= flushPtr)
3460 WalSndCaughtUp = true;
3461
3462 /*
3463 * If we're caught up and have been requested to stop, have WalSndLoop()
3464 * terminate the connection in an orderly manner, after writing out all
3465 * the pending data.
3466 */
3468 got_SIGUSR2 = true;
3469
3470 /* Update shared memory status */
3471 {
3472 WalSnd *walsnd = MyWalSnd;
3473
3474 SpinLockAcquire(&walsnd->mutex);
3475 walsnd->sentPtr = sentPtr;
3476 SpinLockRelease(&walsnd->mutex);
3477 }
3478}
3479
3480/*
3481 * Shutdown if the sender is caught up.
3482 *
3483 * NB: This should only be called when the shutdown signal has been received
3484 * from postmaster.
3485 *
3486 * Note that if we determine that there's still more data to send, this
3487 * function will return control to the caller.
3488 */
3489static void
3491{
3492 XLogRecPtr replicatedPtr;
3493
3494 /* ... let's just be real sure we're caught up ... */
3495 send_data();
3496
3497 /*
3498 * To figure out whether all WAL has successfully been replicated, check
3499 * flush location if valid, write otherwise. Tools like pg_receivewal will
3500 * usually (unless in synchronous mode) return an invalid flush location.
3501 */
3502 replicatedPtr = XLogRecPtrIsInvalid(MyWalSnd->flush) ?
3504
3505 if (WalSndCaughtUp && sentPtr == replicatedPtr &&
3507 {
3508 QueryCompletion qc;
3509
3510 /* Inform the standby that XLOG streaming is done */
3511 SetQueryCompletion(&qc, CMDTAG_COPY, 0);
3512 EndCommand(&qc, DestRemote, false);
3513 pq_flush();
3514
3515 proc_exit(0);
3516 }
3519}
3520
3521/*
3522 * Returns the latest point in WAL that has been safely flushed to disk.
3523 * This should only be called when in recovery.
3524 *
3525 * This is called either by cascading walsender to find WAL position to be sent
3526 * to a cascaded standby or by slot synchronization operation to validate remote
3527 * slot's lsn before syncing it locally.
3528 *
3529 * As a side-effect, *tli is updated to the TLI of the last
3530 * replayed WAL record.
3531 */
3534{
3535 XLogRecPtr replayPtr;
3536 TimeLineID replayTLI;
3537 XLogRecPtr receivePtr;
3539 XLogRecPtr result;
3540
3542
3543 /*
3544 * We can safely send what's already been replayed. Also, if walreceiver
3545 * is streaming WAL from the same timeline, we can send anything that it
3546 * has streamed, but hasn't been replayed yet.
3547 */
3548
3549 receivePtr = GetWalRcvFlushRecPtr(NULL, &receiveTLI);
3550 replayPtr = GetXLogReplayRecPtr(&replayTLI);
3551
3552 if (tli)
3553 *tli = replayTLI;
3554
3555 result = replayPtr;
3556 if (receiveTLI == replayTLI && receivePtr > replayPtr)
3557 result = receivePtr;
3558
3559 return result;
3560}
3561
3562/*
3563 * Request walsenders to reload the currently-open WAL file
3564 */
3565void
3567{
3568 int i;
3569
3570 for (i = 0; i < max_wal_senders; i++)
3571 {
3572 WalSnd *walsnd = &WalSndCtl->walsnds[i];
3573
3574 SpinLockAcquire(&walsnd->mutex);
3575 if (walsnd->pid == 0)
3576 {
3577 SpinLockRelease(&walsnd->mutex);
3578 continue;
3579 }
3580 walsnd->needreload = true;
3581 SpinLockRelease(&walsnd->mutex);
3582 }
3583}
3584
3585/*
3586 * Handle PROCSIG_WALSND_INIT_STOPPING signal.
3587 */
3588void
3590{
3592
3593 /*
3594 * If replication has not yet started, die like with SIGTERM. If
3595 * replication is active, only set a flag and wake up the main loop. It
3596 * will send any outstanding WAL, wait for it to be replicated to the
3597 * standby, and then exit gracefully.
3598 */
3599 if (!replication_active)
3600 kill(MyProcPid, SIGTERM);
3601 else
3602 got_STOPPING = true;
3603}
3604
3605/*
3606 * SIGUSR2: set flag to do a last cycle and shut down afterwards. The WAL
3607 * sender should already have been switched to WALSNDSTATE_STOPPING at
3608 * this point.
3609 */
3610static void
3612{
3613 got_SIGUSR2 = true;
3615}
3616
3617/* Set up signal handlers */
3618void
3620{
3621 /* Set up signal handlers */
3623 pqsignal(SIGINT, StatementCancelHandler); /* query cancel */
3624 pqsignal(SIGTERM, die); /* request shutdown */
3625 /* SIGQUIT handler was already set up by InitPostmasterChild */
3626 InitializeTimeouts(); /* establishes SIGALRM handler */
3627 pqsignal(SIGPIPE, SIG_IGN);
3629 pqsignal(SIGUSR2, WalSndLastCycleHandler); /* request a last cycle and
3630 * shutdown */
3631
3632 /* Reset some signals that are accepted by postmaster but not here */
3633 pqsignal(SIGCHLD, SIG_DFL);
3634}
3635
3636/* Report shared-memory space needed by WalSndShmemInit */
3637Size
3639{
3640 Size size = 0;
3641
3642 size = offsetof(WalSndCtlData, walsnds);
3643 size = add_size(size, mul_size(max_wal_senders, sizeof(WalSnd)));
3644
3645 return size;
3646}
3647
3648/* Allocate and initialize walsender-related shared memory */
3649void
3651{
3652 bool found;
3653 int i;
3654
3656 ShmemInitStruct("Wal Sender Ctl", WalSndShmemSize(), &found);
3657
3658 if (!found)
3659 {
3660 /* First time through, so initialize */
3662
3663 for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
3665
3666 for (i = 0; i < max_wal_senders; i++)
3667 {
3668 WalSnd *walsnd = &WalSndCtl->walsnds[i];
3669
3670 SpinLockInit(&walsnd->mutex);
3671 }
3672
3676 }
3677}
3678
3679/*
3680 * Wake up physical, logical or both kinds of walsenders
3681 *
3682 * The distinction between physical and logical walsenders is done, because:
3683 * - physical walsenders can't send data until it's been flushed
3684 * - logical walsenders on standby can't decode and send data until it's been
3685 * applied
3686 *
3687 * For cascading replication we need to wake up physical walsenders separately
3688 * from logical walsenders (see the comment before calling WalSndWakeup() in
3689 * ApplyWalRecord() for more details).
3690 *
3691 * This will be called inside critical sections, so throwing an error is not
3692 * advisable.
3693 */
3694void
3695WalSndWakeup(bool physical, bool logical)
3696{
3697 /*
3698 * Wake up all the walsenders waiting on WAL being flushed or replayed
3699 * respectively. Note that waiting walsender would have prepared to sleep
3700 * on the CV (i.e., added itself to the CV's waitlist) in WalSndWait()
3701 * before actually waiting.
3702 */
3703 if (physical)
3705
3706 if (logical)
3708}
3709
3710/*
3711 * Wait for readiness on the FeBe socket, or a timeout. The mask should be
3712 * composed of optional WL_SOCKET_WRITEABLE and WL_SOCKET_READABLE flags. Exit
3713 * on postmaster death.
3714 */
3715static void
3716WalSndWait(uint32 socket_events, long timeout, uint32 wait_event)
3717{
3718 WaitEvent event;
3719
3720 ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, socket_events, NULL);
3721
3722 /*
3723 * We use a condition variable to efficiently wake up walsenders in
3724 * WalSndWakeup().
3725 *
3726 * Every walsender prepares to sleep on a shared memory CV. Note that it
3727 * just prepares to sleep on the CV (i.e., adds itself to the CV's
3728 * waitlist), but does not actually wait on the CV (IOW, it never calls
3729 * ConditionVariableSleep()). It still uses WaitEventSetWait() for
3730 * waiting, because we also need to wait for socket events. The processes
3731 * (startup process, walreceiver etc.) wanting to wake up walsenders use
3732 * ConditionVariableBroadcast(), which in turn calls SetLatch(), helping
3733 * walsenders come out of WaitEventSetWait().
3734 *
3735 * This approach is simple and efficient because, one doesn't have to loop
3736 * through all the walsenders slots, with a spinlock acquisition and
3737 * release for every iteration, just to wake up only the waiting
3738 * walsenders. It makes WalSndWakeup() callers' life easy.
3739 *
3740 * XXX: A desirable future improvement would be to add support for CVs
3741 * into WaitEventSetWait().
3742 *
3743 * And, we use separate shared memory CVs for physical and logical
3744 * walsenders for selective wake ups, see WalSndWakeup() for more details.
3745 *
3746 * If the wait event is WAIT_FOR_STANDBY_CONFIRMATION, wait on another CV
3747 * until awakened by physical walsenders after the walreceiver confirms
3748 * the receipt of the LSN.
3749 */
3750 if (wait_event == WAIT_EVENT_WAIT_FOR_STANDBY_CONFIRMATION)
3756
3757 if (WaitEventSetWait(FeBeWaitSet, timeout, &event, 1, wait_event) == 1 &&
3758 (event.events & WL_POSTMASTER_DEATH))
3759 {
3761 proc_exit(1);
3762 }
3763
3765}
3766
3767/*
3768 * Signal all walsenders to move to stopping state.
3769 *
3770 * This will trigger walsenders to move to a state where no further WAL can be
3771 * generated. See this file's header for details.
3772 */
3773void
3775{
3776 int i;
3777
3778 for (i = 0; i < max_wal_senders; i++)
3779 {
3780 WalSnd *walsnd = &WalSndCtl->walsnds[i];
3781 pid_t pid;
3782
3783 SpinLockAcquire(&walsnd->mutex);
3784 pid = walsnd->pid;
3785 SpinLockRelease(&walsnd->mutex);
3786
3787 if (pid == 0)
3788 continue;
3789
3791 }
3792}
3793
3794/*
3795 * Wait that all the WAL senders have quit or reached the stopping state. This
3796 * is used by the checkpointer to control when the shutdown checkpoint can
3797 * safely be performed.
3798 */
3799void
3801{
3802 for (;;)
3803 {
3804 int i;
3805 bool all_stopped = true;
3806
3807 for (i = 0; i < max_wal_senders; i++)
3808 {
3809 WalSnd *walsnd = &WalSndCtl->walsnds[i];
3810
3811 SpinLockAcquire(&walsnd->mutex);
3812
3813 if (walsnd->pid == 0)
3814 {
3815 SpinLockRelease(&walsnd->mutex);
3816 continue;
3817 }
3818
3819 if (walsnd->state != WALSNDSTATE_STOPPING)
3820 {
3821 all_stopped = false;
3822 SpinLockRelease(&walsnd->mutex);
3823 break;
3824 }
3825 SpinLockRelease(&walsnd->mutex);
3826 }
3827
3828 /* safe to leave if confirmation is done for all WAL senders */
3829 if (all_stopped)
3830 return;
3831
3832 pg_usleep(10000L); /* wait for 10 msec */
3833 }
3834}
3835
3836/* Set state for current walsender (only called in walsender) */
3837void
3839{
3840 WalSnd *walsnd = MyWalSnd;
3841
3843
3844 if (walsnd->state == state)
3845 return;
3846
3847 SpinLockAcquire(&walsnd->mutex);
3848 walsnd->state = state;
3849 SpinLockRelease(&walsnd->mutex);
3850}
3851
3852/*
3853 * Return a string constant representing the state. This is used
3854 * in system views, and should *not* be translated.
3855 */
3856static const char *
3858{
3859 switch (state)
3860 {
3862 return "startup";
3863 case WALSNDSTATE_BACKUP:
3864 return "backup";
3866 return "catchup";
3868 return "streaming";
3870 return "stopping";
3871 }
3872 return "UNKNOWN";
3873}
3874
3875static Interval *
3877{
3878 Interval *result = palloc(sizeof(Interval));
3879
3880 result->month = 0;
3881 result->day = 0;
3882 result->time = offset;
3883
3884 return result;
3885}
3886
3887/*
3888 * Returns activity of walsenders, including pids and xlog locations sent to
3889 * standby servers.
3890 */
3891Datum
3893{
3894#define PG_STAT_GET_WAL_SENDERS_COLS 12
3895 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
3896 SyncRepStandbyData *sync_standbys;
3897 int num_standbys;
3898 int i;
3899
3900 InitMaterializedSRF(fcinfo, 0);
3901
3902 /*
3903 * Get the currently active synchronous standbys. This could be out of
3904 * date before we're done, but we'll use the data anyway.
3905 */
3906 num_standbys = SyncRepGetCandidateStandbys(&sync_standbys);
3907
3908 for (i = 0; i < max_wal_senders; i++)
3909 {
3910 WalSnd *walsnd = &WalSndCtl->walsnds[i];
3911 XLogRecPtr sent_ptr;
3913 XLogRecPtr flush;
3914 XLogRecPtr apply;
3915 TimeOffset writeLag;
3916 TimeOffset flushLag;
3917 TimeOffset applyLag;
3918 int priority;
3919 int pid;
3921 TimestampTz replyTime;
3922 bool is_sync_standby;
3924 bool nulls[PG_STAT_GET_WAL_SENDERS_COLS] = {0};
3925 int j;
3926
3927 /* Collect data from shared memory */
3928 SpinLockAcquire(&walsnd->mutex);
3929 if (walsnd->pid == 0)
3930 {
3931 SpinLockRelease(&walsnd->mutex);
3932 continue;
3933 }
3934 pid = walsnd->pid;
3935 sent_ptr = walsnd->sentPtr;
3936 state = walsnd->state;
3937 write = walsnd->write;
3938 flush = walsnd->flush;
3939 apply = walsnd->apply;
3940 writeLag = walsnd->writeLag;
3941 flushLag = walsnd->flushLag;
3942 applyLag = walsnd->applyLag;
3943 priority = walsnd->sync_standby_priority;
3944 replyTime = walsnd->replyTime;
3945 SpinLockRelease(&walsnd->mutex);
3946
3947 /*
3948 * Detect whether walsender is/was considered synchronous. We can
3949 * provide some protection against stale data by checking the PID
3950 * along with walsnd_index.
3951 */
3952 is_sync_standby = false;
3953 for (j = 0; j < num_standbys; j++)
3954 {
3955 if (sync_standbys[j].walsnd_index == i &&
3956 sync_standbys[j].pid == pid)
3957 {
3958 is_sync_standby = true;
3959 break;
3960 }
3961 }
3962
3963 values[0] = Int32GetDatum(pid);
3964
3965 if (!has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))
3966 {
3967 /*
3968 * Only superusers and roles with privileges of pg_read_all_stats
3969 * can see details. Other users only get the pid value to know
3970 * it's a walsender, but no details.
3971 */
3972 MemSet(&nulls[1], true, PG_STAT_GET_WAL_SENDERS_COLS - 1);
3973 }
3974 else
3975 {
3977
3978 if (XLogRecPtrIsInvalid(sent_ptr))
3979 nulls[2] = true;
3980 values[2] = LSNGetDatum(sent_ptr);
3981
3983 nulls[3] = true;
3984 values[3] = LSNGetDatum(write);
3985
3986 if (XLogRecPtrIsInvalid(flush))
3987 nulls[4] = true;
3988 values[4] = LSNGetDatum(flush);
3989
3990 if (XLogRecPtrIsInvalid(apply))
3991 nulls[5] = true;
3992 values[5] = LSNGetDatum(apply);
3993
3994 /*
3995 * Treat a standby such as a pg_basebackup background process
3996 * which always returns an invalid flush location, as an
3997 * asynchronous standby.
3998 */
3999 priority = XLogRecPtrIsInvalid(flush) ? 0 : priority;
4000
4001 if (writeLag < 0)
4002 nulls[6] = true;
4003 else
4005
4006 if (flushLag < 0)
4007 nulls[7] = true;
4008 else
4010
4011 if (applyLag < 0)
4012 nulls[8] = true;
4013 else
4015
4016 values[9] = Int32GetDatum(priority);
4017
4018 /*
4019 * More easily understood version of standby state. This is purely
4020 * informational.
4021 *
4022 * In quorum-based sync replication, the role of each standby
4023 * listed in synchronous_standby_names can be changing very
4024 * frequently. Any standbys considered as "sync" at one moment can
4025 * be switched to "potential" ones at the next moment. So, it's
4026 * basically useless to report "sync" or "potential" as their sync
4027 * states. We report just "quorum" for them.
4028 */
4029 if (priority == 0)
4030 values[10] = CStringGetTextDatum("async");
4031 else if (is_sync_standby)
4033 CStringGetTextDatum("sync") : CStringGetTextDatum("quorum");
4034 else
4035 values[10] = CStringGetTextDatum("potential");
4036
4037 if (replyTime == 0)
4038 nulls[11] = true;
4039 else
4040 values[11] = TimestampTzGetDatum(replyTime);
4041 }
4042
4043 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
4044 values, nulls);
4045 }
4046
4047 return (Datum) 0;
4048}
4049
4050/*
4051 * Send a keepalive message to standby.
4052 *
4053 * If requestReply is set, the message requests the other party to send
4054 * a message back to us, for heartbeat purposes. We also set a flag to
4055 * let nearby code know that we're waiting for that response, to avoid
4056 * repeated requests.
4057 *
4058 * writePtr is the location up to which the WAL is sent. It is essentially
4059 * the same as sentPtr but in some cases, we need to send keep alive before
4060 * sentPtr is updated like when skipping empty transactions.
4061 */
4062static void
4063WalSndKeepalive(bool requestReply, XLogRecPtr writePtr)
4064{
4065 elog(DEBUG2, "sending replication keepalive");
4066
4067 /* construct the message... */
4070 pq_sendint64(&output_message, XLogRecPtrIsInvalid(writePtr) ? sentPtr : writePtr);
4072 pq_sendbyte(&output_message, requestReply ? 1 : 0);
4073
4074 /* ... and send it wrapped in CopyData */
4076
4077 /* Set local flag */
4078 if (requestReply)
4080}
4081
4082/*
4083 * Send keepalive message if too much time has elapsed.
4084 */
4085static void
4087{
4088 TimestampTz ping_time;
4089
4090 /*
4091 * Don't send keepalive messages if timeouts are globally disabled or
4092 * we're doing something not partaking in timeouts.
4093 */
4095 return;
4096
4098 return;
4099
4100 /*
4101 * If half of wal_sender_timeout has lapsed without receiving any reply
4102 * from the standby, send a keep-alive message to the standby requesting
4103 * an immediate reply.
4104 */
4106 wal_sender_timeout / 2);
4107 if (last_processing >= ping_time)
4108 {
4110
4111 /* Try to flush pending output to the client */
4112 if (pq_flush_if_writable() != 0)
4114 }
4115}
4116
4117/*
4118 * Record the end of the WAL and the time it was flushed locally, so that
4119 * LagTrackerRead can compute the elapsed time (lag) when this WAL location is
4120 * eventually reported to have been written, flushed and applied by the
4121 * standby in a reply message.
4122 */
4123static void
4125{
4126 bool buffer_full;
4127 int new_write_head;
4128 int i;
4129
4130 if (!am_walsender)
4131 return;
4132
4133 /*
4134 * If the lsn hasn't advanced since last time, then do nothing. This way
4135 * we only record a new sample when new WAL has been written.
4136 */
4137 if (lag_tracker->last_lsn == lsn)
4138 return;
4139 lag_tracker->last_lsn = lsn;
4140
4141 /*
4142 * If advancing the write head of the circular buffer would crash into any
4143 * of the read heads, then the buffer is full. In other words, the
4144 * slowest reader (presumably apply) is the one that controls the release
4145 * of space.
4146 */
4147 new_write_head = (lag_tracker->write_head + 1) % LAG_TRACKER_BUFFER_SIZE;
4148 buffer_full = false;
4149 for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; ++i)
4150 {
4151 if (new_write_head == lag_tracker->read_heads[i])
4152 buffer_full = true;
4153 }
4154
4155 /*
4156 * If the buffer is full, for now we just rewind by one slot and overwrite
4157 * the last sample, as a simple (if somewhat uneven) way to lower the
4158 * sampling rate. There may be better adaptive compaction algorithms.
4159 */
4160 if (buffer_full)
4161 {
4162 new_write_head = lag_tracker->write_head;
4163 if (lag_tracker->write_head > 0)
4165 else
4167 }
4168
4169 /* Store a sample at the current write head position. */
4171 lag_tracker->buffer[lag_tracker->write_head].time = local_flush_time;
4172 lag_tracker->write_head = new_write_head;
4173}
4174
4175/*
4176 * Find out how much time has elapsed between the moment WAL location 'lsn'
4177 * (or the highest known earlier LSN) was flushed locally and the time 'now'.
4178 * We have a separate read head for each of the reported LSN locations we
4179 * receive in replies from standby; 'head' controls which read head is
4180 * used. Whenever a read head crosses an LSN which was written into the
4181 * lag buffer with LagTrackerWrite, we can use the associated timestamp to
4182 * find out the time this LSN (or an earlier one) was flushed locally, and
4183 * therefore compute the lag.
4184 *
4185 * Return -1 if no new sample data is available, and otherwise the elapsed
4186 * time in microseconds.
4187 */
4188static TimeOffset
4190{
4191 TimestampTz time = 0;
4192
4193 /* Read all unread samples up to this LSN or end of buffer. */
4194 while (lag_tracker->read_heads[head] != lag_tracker->write_head &&
4196 {
4198 lag_tracker->last_read[head] =
4200 lag_tracker->read_heads[head] =
4202 }
4203
4204 /*
4205 * If the lag tracker is empty, that means the standby has processed
4206 * everything we've ever sent so we should now clear 'last_read'. If we
4207 * didn't do that, we'd risk using a stale and irrelevant sample for
4208 * interpolation at the beginning of the next burst of WAL after a period
4209 * of idleness.
4210 */
4212 lag_tracker->last_read[head].time = 0;
4213
4214 if (time > now)
4215 {
4216 /* If the clock somehow went backwards, treat as not found. */
4217 return -1;
4218 }
4219 else if (time == 0)
4220 {
4221 /*
4222 * We didn't cross a time. If there is a future sample that we
4223 * haven't reached yet, and we've already reached at least one sample,
4224 * let's interpolate the local flushed time. This is mainly useful
4225 * for reporting a completely stuck apply position as having
4226 * increasing lag, since otherwise we'd have to wait for it to
4227 * eventually start moving again and cross one of our samples before
4228 * we can show the lag increasing.
4229 */
4231 {
4232 /* There are no future samples, so we can't interpolate. */
4233 return -1;
4234 }
4235 else if (lag_tracker->last_read[head].time != 0)
4236 {
4237 /* We can interpolate between last_read and the next sample. */
4238 double fraction;
4239 WalTimeSample prev = lag_tracker->last_read[head];
4241
4242 if (lsn < prev.lsn)
4243 {
4244 /*
4245 * Reported LSNs shouldn't normally go backwards, but it's
4246 * possible when there is a timeline change. Treat as not
4247 * found.
4248 */
4249 return -1;
4250 }
4251
4252 Assert(prev.lsn < next.lsn);
4253
4254 if (prev.time > next.time)
4255 {
4256 /* If the clock somehow went backwards, treat as not found. */
4257 return -1;
4258 }
4259
4260 /* See how far we are between the previous and next samples. */
4261 fraction =
4262 (double) (lsn - prev.lsn) / (double) (next.lsn - prev.lsn);
4263
4264 /* Scale the local flush time proportionally. */
4265 time = (TimestampTz)
4266 ((double) prev.time + (next.time - prev.time) * fraction);
4267 }
4268 else
4269 {
4270 /*
4271 * We have only a future sample, implying that we were entirely
4272 * caught up but and now there is a new burst of WAL and the
4273 * standby hasn't processed the first sample yet. Until the
4274 * standby reaches the future sample the best we can do is report
4275 * the hypothetical lag if that sample were to be replayed now.
4276 */
4278 }
4279 }
4280
4281 /* Return the elapsed time since local flush time in microseconds. */
4282 Assert(time != 0);
4283 return now - time;
4284}
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5268
void pgaio_error_cleanup(void)
Definition: aio.c:1062
int16 AttrNumber
Definition: attnum.h:21
List * readTimeLineHistory(TimeLineID targetTLI)
Definition: timeline.c:76
TimeLineID tliOfPointInHistory(XLogRecPtr ptr, List *history)
Definition: timeline.c:544
XLogRecPtr tliSwitchPoint(TimeLineID tli, List *history, TimeLineID *nextTLI)
Definition: timeline.c:572
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1757
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1781
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1862
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
void SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
Definition: basebackup.c:990
void AppendIncrementalManifestData(IncrementalBackupInfo *ib, const char *data, int len)
IncrementalBackupInfo * CreateIncrementalBackupInfo(MemoryContext mcxt)
void FinalizeIncrementalManifest(IncrementalBackupInfo *ib)
static int32 next
Definition: blutils.c:224
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define NameStr(name)
Definition: c.h:717
#define pg_noreturn
Definition: c.h:165
#define SIGNAL_ARGS
Definition: c.h:1320
int64_t int64
Definition: c.h:499
#define PG_BINARY
Definition: c.h:1244
#define UINT64_FORMAT
Definition: c.h:521
uint32_t uint32
Definition: c.h:502
#define MemSet(start, val, len)
Definition: c.h:991
uint32 TransactionId
Definition: c.h:623
#define OidIsValid(objectId)
Definition: c.h:746
size_t Size
Definition: c.h:576
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:37
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableInit(ConditionVariable *cv)
void * yyscan_t
Definition: cubedata.h:67
int64 TimestampTz
Definition: timestamp.h:39
int64 TimeOffset
Definition: timestamp.h:40
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3188
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:88
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
void EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
Definition: dest.c:169
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
void EndReplicationCommand(const char *commandTag)
Definition: dest.c:205
@ DestRemote
Definition: dest.h:89
@ DestRemoteSimple
Definition: dest.h:91
@ DestNone
Definition: dest.h:87
struct cursor * cur
Definition: ecpg.c:29
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errcode_for_file_access(void)
Definition: elog.c:877
int errdetail(const char *fmt,...)
Definition: elog.c:1204
bool message_level_is_interesting(int elevel)
Definition: elog.c:273
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define COMMERROR
Definition: elog.h:33
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
#define DEBUG2
Definition: elog.h:29
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
void do_tup_output(TupOutputState *tstate, const Datum *values, const bool *isnull)
Definition: execTuples.c:2464
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2522
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2444
int CloseTransientFile(int fd)
Definition: fd.c:2871
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1089
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2694
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
int MyProcPid
Definition: globals.c:48
struct Latch * MyLatch
Definition: globals.c:64
Oid MyDatabaseId
Definition: globals.c:95
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
void GetPGVariable(const char *name, DestReceiver *dest)
Definition: guc_funcs.c:382
char * application_name
Definition: guc_tables.c:560
Assert(PointerIsAligned(start, uint64))
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
#define write(a, b, c)
Definition: win32.h:14
#define read(a, b, c)
Definition: win32.h:13
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:65
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 j
Definition: isn.c:78
int i
Definition: isn.c:77
void SetLatch(Latch *latch)
Definition: latch.c:288
void ResetLatch(Latch *latch)
Definition: latch.c:372
#define pq_flush()
Definition: libpq.h:46
#define PQ_SMALL_MESSAGE_LIMIT
Definition: libpq.h:30
#define pq_flush_if_writable()
Definition: libpq.h:47
#define pq_is_send_pending()
Definition: libpq.h:48
#define PQ_LARGE_MESSAGE_LIMIT
Definition: libpq.h:31
#define pq_putmessage_noblock(msgtype, s, len)
Definition: libpq.h:51
#define FeBeWaitSetSocketPos
Definition: libpq.h:63
void list_free_deep(List *list)
Definition: list.c:1560
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1818
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:675
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:496
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:631
LogicalDecodingContext * CreateInitDecodingContext(const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:330
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:109
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
void LWLockReleaseAll(void)
Definition: lwlock.c:1953
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:2312
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:414
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1294
char * pstrdup(const char *in)
Definition: mcxt.c:2325
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:668
void pfree(void *pointer)
Definition: mcxt.c:2150
MemoryContext TopMemoryContext
Definition: mcxt.c:165
void * palloc(Size size)
Definition: mcxt.c:1943
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
MemoryContext CacheMemoryContext
Definition: mcxt.c:168
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
#define HOLD_CANCEL_INTERRUPTS()
Definition: miscadmin.h:142
#define RESUME_CANCEL_INTERRUPTS()
Definition: miscadmin.h:144
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
Oid GetUserId(void)
Definition: miscinit.c:520
@ CMD_SELECT
Definition: nodes.h:271
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
#define MAXPGPATH
const void size_t len
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition: pg_list.h:469
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
static bool two_phase
static bool failover
#define die(msg)
static char * buf
Definition: pg_test_fsync.c:72
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_IO
void pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:183
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:165
void MarkPostmasterChildWalSender(void)
Definition: pmsignal.c:309
@ PMSIGNAL_ADVANCE_STATE_MACHINE
Definition: pmsignal.h:43
#define pqsignal
Definition: port.h:531
#define snprintf
Definition: port.h:239
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:3058
CommandDest whereToSendOutput
Definition: postgres.c:91
const char * debug_query_string
Definition: postgres.c:88
uintptr_t Datum
Definition: postgres.h:69
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:35
int pq_getbyte_if_available(unsigned char *c)
Definition: pqcomm.c:1004
int pq_getmessage(StringInfo s, int maxlen)
Definition: pqcomm.c:1204
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:166
void pq_endmsgread(void)
Definition: pqcomm.c:1166
int pq_getbyte(void)
Definition: pqcomm.c:964
void pq_startmsgread(void)
Definition: pqcomm.c:1142
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:579
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:399
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:453
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:314
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 void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PROC_AFFECTS_ALL_HORIZONS
Definition: proc.h:62
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition: procsignal.c:283
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:673
@ PROCSIG_WALSND_INIT_STOPPING
Definition: procsignal.h:35
#define PqMsg_CopyDone
Definition: protocol.h:64
#define PqMsg_CopyData
Definition: protocol.h:65
#define PqMsg_CopyInResponse
Definition: protocol.h:45
#define PqMsg_CopyBothResponse
Definition: protocol.h:54
#define PqMsg_DataRow
Definition: protocol.h:43
#define PqMsg_Terminate
Definition: protocol.h:28
bool update_process_title
Definition: ps_status.c:31
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
bool replication_scanner_is_replication_command(yyscan_t yyscanner)
Definition: repl_scanner.l:299
void replication_scanner_finish(yyscan_t yyscanner)
Definition: repl_scanner.l:284
void replication_scanner_init(const char *str, yyscan_t *yyscannerp)
Definition: repl_scanner.l:268
@ REPLICATION_KIND_PHYSICAL
Definition: replnodes.h:22
@ REPLICATION_KIND_LOGICAL
Definition: replnodes.h:23
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1019
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void CreateAuxProcessResourceOwner(void)
Definition: resowner.c:999
ResourceOwner AuxProcessResourceOwner
Definition: resowner.c:176
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
void pg_usleep(long microsec)
Definition: signal.c:53
void ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
Definition: slot.c:559
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition: slot.c:324
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1061
void ReplicationSlotReserveWal(void)
Definition: slot.c:1452
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1100
void ReplicationSlotPersist(void)
Definition: slot.c:1078
ReplicationSlot * MyReplicationSlot
Definition: slot.c:147
void ReplicationSlotDrop(const char *name, bool nowait)
Definition: slot.c:814
bool SlotExistsInSyncStandbySlots(const char *slot_name)
Definition: slot.c:2772
void ReplicationSlotSave(void)
Definition: slot.c:1043
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:479
void ReplicationSlotAlter(const char *name, const bool *failover, const bool *two_phase)
Definition: slot.c:837
void ReplicationSlotRelease(void)
Definition: slot.c:686
bool StandbySlotsHaveCaughtup(XLogRecPtr wait_for_lsn, int elevel)
Definition: slot.c:2805
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:1156
void ReplicationSlotCleanup(bool synced_only)
Definition: slot.c:775
@ RS_PERSISTENT
Definition: slot.h:38
@ RS_EPHEMERAL
Definition: slot.h:39
@ RS_TEMPORARY
Definition: slot.h:40
#define SlotIsPhysical(slot)
Definition: slot.h:220
#define SlotIsLogical(slot)
Definition: slot.h:221
bool IsSyncingReplicationSlots(void)
Definition: slotsync.c:1653
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition: snapbuild.c:440
const char * SnapBuildExportSnapshot(SnapBuild *builder)
Definition: snapbuild.c:539
void SnapBuildClearExportedSnapshot(void)
Definition: snapbuild.c:600
bool FirstSnapshotSet
Definition: snapmgr.c:192
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
Definition: snapmgr.c:1843
#define SpinLockInit(lock)
Definition: spin.h:57
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:79
char * dbname
Definition: streamutil.c:49
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:337
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
ReplicationKind kind
Definition: replnodes.h:56
char * defname
Definition: parsenodes.h:826
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:229
int read_heads[NUM_SYNC_REP_WAIT_MODE]
Definition: walsender.c:231
WalTimeSample last_read[NUM_SYNC_REP_WAIT_MODE]
Definition: walsender.c:232
int write_head
Definition: walsender.c:230
XLogRecPtr last_lsn
Definition: walsender.c:228
Definition: pg_list.h:54
XLogReaderState * reader
Definition: logical.h:42
struct SnapBuild * snapshot_builder
Definition: logical.h:44
StringInfo out
Definition: logical.h:71
Definition: nodes.h:135
NodeTag type
Definition: nodes.h:136
TransactionId xmin
Definition: proc.h:178
uint8 statusFlags
Definition: proc.h:243
int pgxactoff
Definition: proc.h:185
uint8 * statusFlags
Definition: proc.h:387
TransactionId xmin
Definition: slot.h:89
TransactionId catalog_xmin
Definition: slot.h:97
XLogRecPtr confirmed_flush
Definition: slot.h:111
TransactionId effective_catalog_xmin
Definition: slot.h:182
slock_t mutex
Definition: slot.h:158
bool in_use
Definition: slot.h:161
TransactionId effective_xmin
Definition: slot.h:181
ReplicationSlotPersistentData data
Definition: slot.h:185
TupleDesc setDesc
Definition: execnodes.h:359
Tuplestorestate * setResult
Definition: execnodes.h:358
XLogRecPtr startpoint
Definition: replnodes.h:97
ReplicationKind kind
Definition: replnodes.h:94
TimeLineID timeline
Definition: replnodes.h:96
uint8 syncrep_method
Definition: syncrep.h:68
TimeLineID timeline
Definition: replnodes.h:120
TimeLineID ws_tli
Definition: xlogreader.h:49
uint32 events
Definition: waiteventset.h:62
ConditionVariable wal_confirm_rcv_cv
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
ConditionVariable wal_replay_cv
dlist_head SyncRepQueue[NUM_SYNC_REP_WAIT_MODE]
ConditionVariable wal_flush_cv
TimeOffset writeLag
slock_t mutex
XLogRecPtr flush
XLogRecPtr sentPtr
TimeOffset flushLag
WalSndState state
ReplicationKind kind
XLogRecPtr write
TimeOffset applyLag
int sync_standby_priority
bool needreload
TimestampTz replyTime
XLogRecPtr apply
TimestampTz time
Definition: walsender.c:219
XLogRecPtr lsn
Definition: walsender.c:218
WALSegmentContext segcxt
Definition: xlogreader.h:271
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
WALOpenSegment seg
Definition: xlogreader.h:272
Definition: regguts.h:323
void SyncRepInitConfig(void)
Definition: syncrep.c:445
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
int SyncRepGetCandidateStandbys(SyncRepStandbyData **standbys)
Definition: syncrep.c:754
void SyncRepReleaseWaiters(void)
Definition: syncrep.c:474
#define SYNC_REP_PRIORITY
Definition: syncrep.h:35
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:27
#define SyncRepRequested()
Definition: syncrep.h:18
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:23
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:24
#define SYNC_REP_WAIT_APPLY
Definition: syncrep.h:25
void InitializeTimeouts(void)
Definition: timeout.c:470
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define InvalidTransactionId
Definition: transam.h:31
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:175
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:911
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
char data[BLCKSZ]
Definition: c.h:1090
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85
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
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: waiteventset.c:655
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
#define WL_SOCKET_READABLE
Definition: waiteventset.h:35
#define WL_POSTMASTER_DEATH
Definition: waiteventset.h:38
#define WL_SOCKET_WRITEABLE
Definition: waiteventset.h:36
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
static void ProcessPendingWrites(void)
Definition: walsender.c:1595
static XLogRecPtr sentPtr
Definition: walsender.c:170
#define READ_REPLICATION_SLOT_COLS
static void AlterReplicationSlot(AlterReplicationSlotCmd *cmd)
Definition: walsender.c:1391
static void WalSndWait(uint32 socket_events, long timeout, uint32 wait_event)
Definition: walsender.c:3716
static void WalSndLastCycleHandler(SIGNAL_ARGS)
Definition: walsender.c:3611
static volatile sig_atomic_t got_SIGUSR2
Definition: walsender.c:202
static void WalSndCheckTimeOut(void)
Definition: walsender.c:2756
static void XLogSendPhysical(void)
Definition: walsender.c:3095
static void ProcessRepliesIfAny(void)
Definition: walsender.c:2226
static bool waiting_for_ping_response
Definition: walsender.c:187
void PhysicalWakeupLogicalWalSnd(void)
Definition: walsender.c:1714
static void SendTimeLineHistory(TimeLineHistoryCmd *cmd)
Definition: walsender.c:563
void WalSndErrorCleanup(void)
Definition: walsender.c:330
static void InitWalSenderSlot(void)
Definition: walsender.c:2925
static void parseCreateReplSlotOptions(CreateReplicationSlotCmd *cmd, bool *reserve_wal, CRSSnapshotAction *snapshot_action, bool *two_phase, bool *failover)
Definition: walsender.c:1100
WalSnd * MyWalSnd
Definition: walsender.c:117
static void ProcessStandbyHSFeedbackMessage(void)
Definition: walsender.c:2588
static void ReadReplicationSlot(ReadReplicationSlotCmd *cmd)
Definition: walsender.c:464
static StringInfoData tmpbuf
Definition: walsender.c:175
static void PhysicalReplicationSlotNewXmin(TransactionId feedbackXmin, TransactionId feedbackCatalogXmin)
Definition: walsender.c:2508
static LagTracker * lag_tracker
Definition: walsender.c:235
static void PhysicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: walsender.c:2370
static void IdentifySystem(void)
Definition: walsender.c:383
static void WalSndSegmentOpen(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: walsender.c:3017
static StringInfoData reply_message
Definition: walsender.c:174
static void WalSndKeepaliveIfNecessary(void)
Definition: walsender.c:4086
bool am_walsender
Definition: walsender.c:120
void WalSndSetState(WalSndState state)
Definition: walsender.c:3838
static StringInfoData output_message
Definition: walsender.c:173
static TimeLineID sendTimeLine
Definition: walsender.c:161
static bool HandleUploadManifestPacket(StringInfo buf, off_t *offset, IncrementalBackupInfo *ib)
Definition: walsender.c:719
static void WalSndLoop(WalSndSendDataCallback send_data)
Definition: walsender.c:2783
static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Definition: walsender.c:1553
void WalSndWakeup(bool physical, bool logical)
Definition: walsender.c:3695
static LogicalDecodingContext * logical_decoding_ctx
Definition: walsender.c:213
static void XLogSendLogical(void)
Definition: walsender.c:3405
void WalSndShmemInit(void)
Definition: walsender.c:3650
bool am_db_walsender
Definition: walsender.c:123
static volatile sig_atomic_t replication_active
Definition: walsender.c:211
static void UploadManifest(void)
Definition: walsender.c:653
bool wake_wal_senders
Definition: walsender.c:135
static volatile sig_atomic_t got_STOPPING
Definition: walsender.c:203
int max_wal_senders
Definition: walsender.c:126
static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch)
Definition: walsender.c:2557
static void WalSndUpdateProgress(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool skipped_xact)
Definition: walsender.c:1649
bool exec_replication_command(const char *cmd_string)
Definition: walsender.c:1970
#define WALSND_LOGICAL_LAG_TRACK_INTERVAL_MS
static bool NeedToWaitForStandbys(XLogRecPtr flushed_lsn, uint32 *wait_event)
Definition: walsender.c:1739
void InitWalSender(void)
Definition: walsender.c:283
#define PG_STAT_GET_WAL_SENDERS_COLS
void(* WalSndSendDataCallback)(void)
Definition: walsender.c:241
Datum pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
Definition: walsender.c:3892
void WalSndInitStopping(void)
Definition: walsender.c:3774
void WalSndWaitStopping(void)
Definition: walsender.c:3800
static bool sendTimeLineIsHistoric
Definition: walsender.c:163
void WalSndRqstFileReload(void)
Definition: walsender.c:3566
static XLogRecPtr WalSndWaitForWal(XLogRecPtr loc)
Definition: walsender.c:1799
bool am_cascading_walsender
Definition: walsender.c:121
static TimestampTz last_processing
Definition: walsender.c:178
static bool NeedToWaitForWal(XLogRecPtr target_lsn, XLogRecPtr flushed_lsn, uint32 *wait_event)
Definition: walsender.c:1771
Size WalSndShmemSize(void)
Definition: walsender.c:3638
bool log_replication_commands
Definition: walsender.c:130
void HandleWalSndInitStopping(void)
Definition: walsender.c:3589
static TimeLineID sendTimeLineNextTLI
Definition: walsender.c:162
static MemoryContext uploaded_manifest_mcxt
Definition: walsender.c:153
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:1177
static int logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: walsender.c:1027
static void ProcessStandbyReplyMessage(void)
Definition: walsender.c:2403
static void WalSndKeepalive(bool requestReply, XLogRecPtr writePtr)
Definition: walsender.c:4063
static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time)
Definition: walsender.c:4124
#define WALSENDER_STATS_FLUSH_INTERVAL
Definition: walsender.c:100
void WalSndSignals(void)
Definition: walsender.c:3619
static bool streamingDoneSending
Definition: walsender.c:195
static void StartLogicalReplication(StartReplicationCmd *cmd)
Definition: walsender.c:1433
static IncrementalBackupInfo * uploaded_manifest
Definition: walsender.c:152
static pg_noreturn void WalSndShutdown(void)
Definition: walsender.c:366
static void WalSndKill(int code, Datum arg)
Definition: walsender.c:3001
int wal_sender_timeout
Definition: walsender.c:128
#define MAX_SEND_SIZE
Definition: walsender.c:111
static Interval * offset_to_interval(TimeOffset offset)
Definition: walsender.c:3876
static bool WalSndCaughtUp
Definition: walsender.c:199
static XLogRecPtr sendTimeLineValidUpto
Definition: walsender.c:164
static void ProcessStandbyMessage(void)
Definition: walsender.c:2339
static void WalSndPrepareWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Definition: walsender.c:1526
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1382
#define LAG_TRACKER_BUFFER_SIZE
Definition: walsender.c:223
static const char * WalSndGetStateString(WalSndState state)
Definition: walsender.c:3857
static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now)
Definition: walsender.c:4189
static long WalSndComputeSleeptime(TimestampTz now)
Definition: walsender.c:2712
static bool streamingDoneReceiving
Definition: walsender.c:196
static void StartReplication(StartReplicationCmd *cmd)
Definition: walsender.c:795
static void WalSndDone(WalSndSendDataCallback send_data)
Definition: walsender.c:3490
static XLogReaderState * xlogreader
Definition: walsender.c:142
static TimestampTz last_reply_timestamp
Definition: walsender.c:184
XLogRecPtr GetStandbyFlushRecPtr(TimeLineID *tli)
Definition: walsender.c:3533
WalSndCtlData * WalSndCtl
Definition: walsender.c:114
CRSSnapshotAction
Definition: walsender.h:21
@ CRS_USE_SNAPSHOT
Definition: walsender.h:24
@ CRS_NOEXPORT_SNAPSHOT
Definition: walsender.h:23
@ CRS_EXPORT_SNAPSHOT
Definition: walsender.h:22
#define SYNC_STANDBY_DEFINED
WalSndState
@ WALSNDSTATE_STREAMING
@ WALSNDSTATE_BACKUP
@ WALSNDSTATE_CATCHUP
@ WALSNDSTATE_STARTUP
@ WALSNDSTATE_STOPPING
int replication_yyparse(Node **replication_parse_result_p, yyscan_t yyscanner)
#define SIGCHLD
Definition: win32_port.h:168
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define kill(pid, sig)
Definition: win32_port.h:493
#define SIGUSR1
Definition: win32_port.h:170
#define SIGUSR2
Definition: win32_port.h:171
static const unsigned __int64 epoch
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4989
bool XactReadOnly
Definition: xact.c:82
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3648
void StartTransactionCommand(void)
Definition: xact.c:3059
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:407
int XactIsoLevel
Definition: xact.c:79
bool IsSubTransaction(void)
Definition: xact.c:5044
bool IsTransactionBlock(void)
Definition: xact.c:4971
void CommitTransactionCommand(void)
Definition: xact.c:3157
#define XACT_REPEATABLE_READ
Definition: xact.h:38
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4734
bool RecoveryInProgress(void)
Definition: xlog.c:6522
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6708
Size WALReadFromBuffers(char *dstbuf, XLogRecPtr startptr, Size count, TimeLineID tli)
Definition: xlog.c:1759
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3866
int wal_segment_size
Definition: xlog.c:143
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6687
bool XLogBackgroundFlush(void)
Definition: xlog.c:3111
#define MAXFNAMELEN
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
static void TLHistoryFilePath(char *path, TimeLineID tli)
static void TLHistoryFileName(char *fname, TimeLineID tli)
#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
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107
bool WALRead(XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
Definition: xlogreader.c:1504
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:390
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:232
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
static TimeLineID receiveTLI
Definition: xlogrecovery.c:264
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:831
void XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wantLength, TimeLineID currTLI)
Definition: xlogutils.c:707
void WALReadRaiseError(WALReadError *errinfo)
Definition: xlogutils.c:1011