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