Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * walreceiverfuncs.c
4 : : *
5 : : * This file contains functions used by the startup process to communicate
6 : : * with the walreceiver process. Functions implementing walreceiver itself
7 : : * are in walreceiver.c.
8 : : *
9 : : * Portions Copyright (c) 2010-2026, PostgreSQL Global Development Group
10 : : *
11 : : *
12 : : * IDENTIFICATION
13 : : * src/backend/replication/walreceiverfuncs.c
14 : : *
15 : : *-------------------------------------------------------------------------
16 : : */
17 : : #include "postgres.h"
18 : :
19 : : #include <sys/stat.h>
20 : : #include <sys/time.h>
21 : : #include <time.h>
22 : : #include <unistd.h>
23 : : #include <signal.h>
24 : :
25 : : #include "access/xlog_internal.h"
26 : : #include "access/xlogrecovery.h"
27 : : #include "pgstat.h"
28 : : #include "replication/walreceiver.h"
29 : : #include "storage/pmsignal.h"
30 : : #include "storage/proc.h"
31 : : #include "storage/shmem.h"
32 : : #include "storage/subsystems.h"
33 : : #include "utils/timestamp.h"
34 : : #include "utils/wait_event.h"
35 : :
36 : : WalRcvData *WalRcv = NULL;
37 : :
38 : : static void WalRcvShmemRequest(void *arg);
39 : : static void WalRcvShmemInit(void *arg);
40 : :
41 : : const ShmemCallbacks WalRcvShmemCallbacks = {
42 : : .request_fn = WalRcvShmemRequest,
43 : : .init_fn = WalRcvShmemInit,
44 : : };
45 : :
46 : : /*
47 : : * How long to wait for walreceiver to start up after requesting
48 : : * postmaster to launch it. In seconds.
49 : : */
50 : : #define WALRCV_STARTUP_TIMEOUT 10
51 : :
52 : : /* Register shared memory space needed by walreceiver */
53 : : static void
29 heikki.linnakangas@i 54 :GNC 1244 : WalRcvShmemRequest(void *arg)
55 : : {
56 : 1244 : ShmemRequestStruct(.name = "Wal Receiver Ctl",
57 : : .size = sizeof(WalRcvData),
58 : : .ptr = (void **) &WalRcv,
59 : : );
5954 heikki.linnakangas@i 60 :GIC 1244 : }
61 : :
62 : : /* Initialize walreceiver-related shared memory */
63 : : static void
29 heikki.linnakangas@i 64 :GNC 1241 : WalRcvShmemInit(void *arg)
65 : : {
66 [ + - + - : 1241 : MemSet(WalRcv, 0, sizeof(WalRcvData));
+ - - + -
- ]
67 : 1241 : WalRcv->walRcvState = WALRCV_STOPPED;
68 : 1241 : ConditionVariableInit(&WalRcv->walRcvStoppedCV);
69 : 1241 : SpinLockInit(&WalRcv->mutex);
70 : 1241 : pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
71 : 1241 : WalRcv->procno = INVALID_PROC_NUMBER;
5954 heikki.linnakangas@i 72 :CBC 1241 : }
73 : :
74 : : /* Is walreceiver running (or starting up)? */
75 : : bool
4891 76 : 1122 : WalRcvRunning(void)
77 : : {
3864 rhaas@postgresql.org 78 : 1122 : WalRcvData *walrcv = WalRcv;
79 : : WalRcvState state;
80 : : pg_time_t startTime;
81 : :
5954 heikki.linnakangas@i 82 [ - + ]: 1122 : SpinLockAcquire(&walrcv->mutex);
83 : :
5942 84 : 1122 : state = walrcv->walRcvState;
85 : 1122 : startTime = walrcv->startTime;
86 : :
87 : 1122 : SpinLockRelease(&walrcv->mutex);
88 : :
89 : : /*
90 : : * If it has taken too long for walreceiver to start up, give up. Setting
91 : : * the state to STOPPED ensures that if walreceiver later does start up
92 : : * after all, it will see that it's not supposed to be running and die
93 : : * without doing anything.
94 : : */
95 [ + + ]: 1122 : if (state == WALRCV_STARTING)
96 : : {
5912 bruce@momjian.us 97 : 1 : pg_time_t now = (pg_time_t) time(NULL);
98 : :
5942 heikki.linnakangas@i 99 [ - + ]: 1 : if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
100 : : {
1880 tmunro@postgresql.or 101 :UBC 0 : bool stopped = false;
102 : :
103 [ # # ]: 0 : SpinLockAcquire(&walrcv->mutex);
5942 heikki.linnakangas@i 104 [ # # ]: 0 : if (walrcv->walRcvState == WALRCV_STARTING)
105 : : {
106 : 0 : state = walrcv->walRcvState = WALRCV_STOPPED;
1880 tmunro@postgresql.or 107 : 0 : stopped = true;
108 : : }
5942 heikki.linnakangas@i 109 : 0 : SpinLockRelease(&walrcv->mutex);
110 : :
1880 tmunro@postgresql.or 111 [ # # ]: 0 : if (stopped)
112 : 0 : ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
113 : : }
114 : : }
115 : :
5942 heikki.linnakangas@i 116 [ + + ]:CBC 1122 : if (state != WALRCV_STOPPED)
117 : 47 : return true;
118 : : else
119 : 1075 : return false;
120 : : }
121 : :
122 : : /* Return the state of the walreceiver. */
123 : : WalRcvState
182 michael@paquier.xyz 124 :GNC 152 : WalRcvGetState(void)
125 : : {
126 : 152 : WalRcvData *walrcv = WalRcv;
127 : : WalRcvState state;
128 : :
129 : 152 : SpinLockAcquire(&walrcv->mutex);
130 : 152 : state = walrcv->walRcvState;
131 : 152 : SpinLockRelease(&walrcv->mutex);
132 : :
133 : 152 : return state;
134 : : }
135 : :
136 : : /*
137 : : * Is walreceiver running and streaming (or at least attempting to connect,
138 : : * or starting up)?
139 : : */
140 : : bool
4891 heikki.linnakangas@i 141 :CBC 35866 : WalRcvStreaming(void)
142 : : {
3864 rhaas@postgresql.org 143 : 35866 : WalRcvData *walrcv = WalRcv;
144 : : WalRcvState state;
145 : : pg_time_t startTime;
146 : :
4891 heikki.linnakangas@i 147 [ - + ]: 35866 : SpinLockAcquire(&walrcv->mutex);
148 : :
149 : 35866 : state = walrcv->walRcvState;
150 : 35866 : startTime = walrcv->startTime;
151 : :
152 : 35866 : SpinLockRelease(&walrcv->mutex);
153 : :
154 : : /*
155 : : * If it has taken too long for walreceiver to start up, give up. Setting
156 : : * the state to STOPPED ensures that if walreceiver later does start up
157 : : * after all, it will see that it's not supposed to be running and die
158 : : * without doing anything.
159 : : */
160 [ + + ]: 35866 : if (state == WALRCV_STARTING)
161 : : {
162 : 281 : pg_time_t now = (pg_time_t) time(NULL);
163 : :
164 [ - + ]: 281 : if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
165 : : {
1880 tmunro@postgresql.or 166 :UBC 0 : bool stopped = false;
167 : :
168 [ # # ]: 0 : SpinLockAcquire(&walrcv->mutex);
4891 heikki.linnakangas@i 169 [ # # ]: 0 : if (walrcv->walRcvState == WALRCV_STARTING)
170 : : {
171 : 0 : state = walrcv->walRcvState = WALRCV_STOPPED;
1880 tmunro@postgresql.or 172 : 0 : stopped = true;
173 : : }
4891 heikki.linnakangas@i 174 : 0 : SpinLockRelease(&walrcv->mutex);
175 : :
1880 tmunro@postgresql.or 176 [ # # ]: 0 : if (stopped)
177 : 0 : ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
178 : : }
179 : : }
180 : :
4891 heikki.linnakangas@i 181 [ + + + + :CBC 35866 : if (state == WALRCV_STREAMING || state == WALRCV_STARTING ||
+ + ]
102 michael@paquier.xyz 182 [ + + ]:GNC 3256 : state == WALRCV_CONNECTING || state == WALRCV_RESTARTING)
4891 heikki.linnakangas@i 183 :CBC 32618 : return true;
184 : : else
185 : 3248 : return false;
186 : : }
187 : :
188 : : /*
189 : : * Stop walreceiver (if running) and wait for it to die.
190 : : * Executed by the Startup process.
191 : : */
192 : : void
5954 193 : 1070 : ShutdownWalRcv(void)
194 : : {
3864 rhaas@postgresql.org 195 : 1070 : WalRcvData *walrcv = WalRcv;
5912 bruce@momjian.us 196 : 1070 : pid_t walrcvpid = 0;
1880 tmunro@postgresql.or 197 : 1070 : bool stopped = false;
198 : :
199 : : /*
200 : : * Request walreceiver to stop. Walreceiver will switch to WALRCV_STOPPED
201 : : * mode once it's finished, and will also request postmaster to not
202 : : * restart itself.
203 : : */
5954 heikki.linnakangas@i 204 [ - + ]: 1070 : SpinLockAcquire(&walrcv->mutex);
5912 bruce@momjian.us 205 [ + + + - : 1070 : switch (walrcv->walRcvState)
- ]
206 : : {
5942 heikki.linnakangas@i 207 : 1028 : case WALRCV_STOPPED:
208 : 1028 : break;
209 : 2 : case WALRCV_STARTING:
210 : 2 : walrcv->walRcvState = WALRCV_STOPPED;
1880 tmunro@postgresql.or 211 : 2 : stopped = true;
5942 heikki.linnakangas@i 212 : 2 : break;
213 : :
102 michael@paquier.xyz 214 :GNC 40 : case WALRCV_CONNECTING:
4891 heikki.linnakangas@i 215 :ECB (30) : case WALRCV_STREAMING:
216 : : case WALRCV_WAITING:
217 : : case WALRCV_RESTARTING:
5942 heikki.linnakangas@i 218 :CBC 40 : walrcv->walRcvState = WALRCV_STOPPING;
219 : : pg_fallthrough;
220 : 40 : case WALRCV_STOPPING:
221 : 40 : walrcvpid = walrcv->pid;
222 : 40 : break;
223 : : }
5954 224 : 1070 : SpinLockRelease(&walrcv->mutex);
225 : :
226 : : /* Unnecessary but consistent. */
1880 tmunro@postgresql.or 227 [ + + ]: 1070 : if (stopped)
228 : 2 : ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
229 : :
230 : : /*
231 : : * Signal walreceiver process if it was still running.
232 : : */
5954 heikki.linnakangas@i 233 [ + + ]: 1070 : if (walrcvpid != 0)
234 : 40 : kill(walrcvpid, SIGTERM);
235 : :
236 : : /*
237 : : * Wait for walreceiver to acknowledge its death by setting state to
238 : : * WALRCV_STOPPED.
239 : : */
1880 tmunro@postgresql.or 240 : 1070 : ConditionVariablePrepareToSleep(&walrcv->walRcvStoppedCV);
4891 heikki.linnakangas@i 241 [ + + ]: 1109 : while (WalRcvRunning())
1880 tmunro@postgresql.or 242 : 39 : ConditionVariableSleep(&walrcv->walRcvStoppedCV,
243 : : WAIT_EVENT_WAL_RECEIVER_EXIT);
244 : 1070 : ConditionVariableCancelSleep();
5954 heikki.linnakangas@i 245 : 1070 : }
246 : :
247 : : /*
248 : : * Request postmaster to start walreceiver.
249 : : *
250 : : * "recptr" indicates the position where streaming should begin. "conninfo"
251 : : * is a libpq connection string to use. "slotname" is, optionally, the name
252 : : * of a replication slot to acquire. "create_temp_slot" indicates to create
253 : : * a temporary slot when no "slotname" is given.
254 : : *
255 : : * WAL receivers do not directly load GUC parameters used for the connection
256 : : * to the primary, and rely on the values passed down by the caller of this
257 : : * routine instead. Hence, the addition of any new parameters should happen
258 : : * through this code path.
259 : : */
260 : : void
4477 rhaas@postgresql.org 261 : 194 : RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo,
262 : : const char *slotname, bool create_temp_slot)
263 : : {
3864 264 : 194 : WalRcvData *walrcv = WalRcv;
4891 heikki.linnakangas@i 265 : 194 : bool launch = false;
5912 bruce@momjian.us 266 : 194 : pg_time_t now = (pg_time_t) time(NULL);
267 : : ProcNumber walrcv_proc;
268 : :
269 : : /*
270 : : * We always start at the beginning of the segment. That prevents a broken
271 : : * segment (i.e., with no records in the first half of a segment) from
272 : : * being created by XLOG streaming, which might cause trouble later on if
273 : : * the segment is e.g archived.
274 : : */
3150 andres@anarazel.de 275 [ + - ]: 194 : if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
276 : 194 : recptr -= XLogSegmentOffset(recptr, wal_segment_size);
277 : :
5785 tgl@sss.pgh.pa.us 278 [ - + ]: 194 : SpinLockAcquire(&walrcv->mutex);
279 : :
280 : : /* It better be stopped if we try to restart it */
4891 heikki.linnakangas@i 281 [ + + - + ]: 194 : Assert(walrcv->walRcvState == WALRCV_STOPPED ||
282 : : walrcv->walRcvState == WALRCV_WAITING);
283 : :
5954 284 [ + - ]: 194 : if (conninfo != NULL)
447 peter@eisentraut.org 285 : 194 : strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO);
286 : : else
5954 heikki.linnakangas@i 287 :UBC 0 : walrcv->conninfo[0] = '\0';
288 : :
289 : : /*
290 : : * Use configured replication slot if present, and ignore the value of
291 : : * create_temp_slot as the slot name should be persistent. Otherwise, use
292 : : * create_temp_slot to determine whether this WAL receiver should create a
293 : : * temporary slot by itself and use it, or not.
294 : : */
2230 alvherre@alvh.no-ip. 295 [ + - + + ]:CBC 194 : if (slotname != NULL && slotname[0] != '\0')
296 : : {
447 peter@eisentraut.org 297 : 56 : strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
2230 alvherre@alvh.no-ip. 298 : 56 : walrcv->is_temp_slot = false;
299 : : }
300 : : else
301 : : {
4477 rhaas@postgresql.org 302 : 138 : walrcv->slotname[0] = '\0';
2230 alvherre@alvh.no-ip. 303 : 138 : walrcv->is_temp_slot = create_temp_slot;
304 : : }
305 : :
4891 heikki.linnakangas@i 306 [ + + ]: 194 : if (walrcv->walRcvState == WALRCV_STOPPED)
307 : : {
308 : 186 : launch = true;
309 : 186 : walrcv->walRcvState = WALRCV_STARTING;
310 : : }
311 : : else
312 : 8 : walrcv->walRcvState = WALRCV_RESTARTING;
5942 313 : 194 : walrcv->startTime = now;
314 : :
315 : : /*
316 : : * If this is the first startup of walreceiver (on this timeline),
317 : : * initialize flushedUpto and latestChunkStart to the starting point.
318 : : */
180 alvherre@kurilemu.de 319 [ + + - + ]:GNC 194 : if (!XLogRecPtrIsValid(walrcv->receiveStart) || walrcv->receivedTLI != tli)
320 : : {
2218 tmunro@postgresql.or 321 :CBC 114 : walrcv->flushedUpto = recptr;
4745 heikki.linnakangas@i 322 : 114 : walrcv->receivedTLI = tli;
5544 323 : 114 : walrcv->latestChunkStart = recptr;
324 : :
325 : : /*
326 : : * Pairs with pg_atomic_read_membarrier_u64() in
327 : : * GetWalRcvWriteRecPtr().
328 : : */
2 akorotkov@postgresql 329 :GNC 114 : pg_atomic_write_membarrier_u64(&walrcv->writtenUpto, recptr);
330 : : }
5544 heikki.linnakangas@i 331 :CBC 194 : walrcv->receiveStart = recptr;
4891 332 : 194 : walrcv->receiveStartTLI = tli;
333 : :
550 334 : 194 : walrcv_proc = walrcv->procno;
335 : :
5954 336 : 194 : SpinLockRelease(&walrcv->mutex);
337 : :
4891 338 [ + + ]: 194 : if (launch)
339 : 186 : SendPostmasterSignal(PMSIGNAL_START_WALRECEIVER);
550 340 [ + - ]: 8 : else if (walrcv_proc != INVALID_PROC_NUMBER)
341 : 8 : SetLatch(&GetPGProcByNumber(walrcv_proc)->procLatch);
5954 342 : 194 : }
343 : :
344 : : /*
345 : : * Returns the last+1 byte position that walreceiver has flushed.
346 : : *
347 : : * Optionally, returns the previous chunk start, that is the first byte
348 : : * written in the most recent walreceiver flush cycle. Callers not
349 : : * interested in that value may pass NULL for latestChunkStart. Same for
350 : : * receiveTLI.
351 : : */
352 : : XLogRecPtr
2218 tmunro@postgresql.or 353 : 32254 : GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
354 : : {
3864 rhaas@postgresql.org 355 : 32254 : WalRcvData *walrcv = WalRcv;
356 : : XLogRecPtr recptr;
357 : :
5954 heikki.linnakangas@i 358 [ - + ]: 32254 : SpinLockAcquire(&walrcv->mutex);
2218 tmunro@postgresql.or 359 : 32254 : recptr = walrcv->flushedUpto;
5785 tgl@sss.pgh.pa.us 360 [ + + ]: 32254 : if (latestChunkStart)
361 : 30881 : *latestChunkStart = walrcv->latestChunkStart;
4891 heikki.linnakangas@i 362 [ + + ]: 32254 : if (receiveTLI)
363 : 31998 : *receiveTLI = walrcv->receivedTLI;
5954 364 : 32254 : SpinLockRelease(&walrcv->mutex);
365 : :
366 : 32254 : return recptr;
367 : : }
368 : :
369 : : /*
370 : : * Returns the last+1 byte position that walreceiver has written.
371 : : *
372 : : * Use pg_atomic_read_membarrier_u64() to ensure that callers see up-to-date
373 : : * shared memory state, matching the barrier semantics provided by the
374 : : * spinlock in GetWalRcvFlushRecPtr() and other LSN-position functions.
375 : : */
376 : : XLogRecPtr
2218 tmunro@postgresql.or 377 :GBC 49 : GetWalRcvWriteRecPtr(void)
378 : : {
379 : 49 : WalRcvData *walrcv = WalRcv;
380 : :
2 akorotkov@postgresql 381 :GNC 49 : return pg_atomic_read_membarrier_u64(&walrcv->writtenUpto);
382 : : }
383 : :
384 : : /*
385 : : * Returns the replication apply delay in ms or -1
386 : : * if the apply delay info is not available
387 : : */
388 : : int
5239 simon@2ndQuadrant.co 389 :CBC 417 : GetReplicationApplyDelay(void)
390 : : {
3864 rhaas@postgresql.org 391 : 417 : WalRcvData *walrcv = WalRcv;
392 : : XLogRecPtr receivePtr;
393 : : XLogRecPtr replayPtr;
394 : : TimestampTz chunkReplayStartTime;
395 : :
5239 simon@2ndQuadrant.co 396 [ - + ]: 417 : SpinLockAcquire(&walrcv->mutex);
2218 tmunro@postgresql.or 397 : 417 : receivePtr = walrcv->flushedUpto;
5239 simon@2ndQuadrant.co 398 : 417 : SpinLockRelease(&walrcv->mutex);
399 : :
4884 heikki.linnakangas@i 400 : 417 : replayPtr = GetXLogReplayRecPtr(NULL);
401 : :
4876 alvherre@alvh.no-ip. 402 [ + + ]: 417 : if (receivePtr == replayPtr)
5239 simon@2ndQuadrant.co 403 : 134 : return 0;
404 : :
3339 peter_e@gmx.net 405 : 283 : chunkReplayStartTime = GetCurrentChunkReplayStartTime();
406 : :
407 [ + + ]: 283 : if (chunkReplayStartTime == 0)
4070 ishii@postgresql.org 408 : 3 : return -1;
409 : :
2002 tgl@sss.pgh.pa.us 410 : 280 : return TimestampDifferenceMilliseconds(chunkReplayStartTime,
411 : : GetCurrentTimestamp());
412 : : }
413 : :
414 : : /*
415 : : * Returns the network latency in ms, note that this includes any
416 : : * difference in clock settings between the servers, as well as timezone.
417 : : */
418 : : int
5239 simon@2ndQuadrant.co 419 : 417 : GetReplicationTransferLatency(void)
420 : : {
3864 rhaas@postgresql.org 421 : 417 : WalRcvData *walrcv = WalRcv;
422 : : TimestampTz lastMsgSendTime;
423 : : TimestampTz lastMsgReceiptTime;
424 : :
5239 simon@2ndQuadrant.co 425 [ - + ]: 417 : SpinLockAcquire(&walrcv->mutex);
426 : 417 : lastMsgSendTime = walrcv->lastMsgSendTime;
427 : 417 : lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
428 : 417 : SpinLockRelease(&walrcv->mutex);
429 : :
2002 tgl@sss.pgh.pa.us 430 : 417 : return TimestampDifferenceMilliseconds(lastMsgSendTime,
431 : : lastMsgReceiptTime);
432 : : }
|