Debian Codegraph @

src/backend/replication/walsender.c

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