Debian Codegraph @

src/backend/replication/walreceiverfuncs.c

raw
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-2025, 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 "utils/timestamp.h"
33
34 WalRcvData *WalRcv = NULL;
35
36 /*
37 * How long to wait for walreceiver to start up after requesting
38 * postmaster to launch it. In seconds.
39 */
40 #define WALRCV_STARTUP_TIMEOUT 10
41
42 /* Report shared memory space needed by WalRcvShmemInit */
43 Size
44 WalRcvShmemSize(void)
45 {
46 Size size = 0;
47
48 size = add_size(size, sizeof(WalRcvData));
49
50 return size;
51 }
52
53 /* Allocate and initialize walreceiver-related shared memory */
54 void
55 WalRcvShmemInit(void)
56 {
57 bool found;
58
59 WalRcv = (WalRcvData *)
60 ShmemInitStruct("Wal Receiver Ctl", WalRcvShmemSize(), &found);
61
62 if (!found)
63 {
64 /* First time through, so initialize */
65 MemSet(WalRcv, 0, WalRcvShmemSize());
66 WalRcv->walRcvState = WALRCV_STOPPED;
67 ConditionVariableInit(&WalRcv->walRcvStoppedCV);
68 SpinLockInit(&WalRcv->mutex);
69 pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
70 WalRcv->procno = INVALID_PROC_NUMBER;
71 }
72 }
73
74 /* Is walreceiver running (or starting up)? */
75 bool
76 WalRcvRunning(void)
77 {
78 WalRcvData *walrcv = WalRcv;
79 WalRcvState state;
80 pg_time_t startTime;
81
82 SpinLockAcquire(&walrcv->mutex);
83
84 state = walrcv->walRcvState;
85 startTime = walrcv->startTime;
86
87 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 if (state == WALRCV_STARTING)
96 {
97 pg_time_t now = (pg_time_t) time(NULL);
98
99 if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
100 {
101 bool stopped = false;
102
103 SpinLockAcquire(&walrcv->mutex);
104 if (walrcv->walRcvState == WALRCV_STARTING)
105 {
106 state = walrcv->walRcvState = WALRCV_STOPPED;
107 stopped = true;
108 }
109 SpinLockRelease(&walrcv->mutex);
110
111 if (stopped)
112 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
113 }
114 }
115
116 if (state != WALRCV_STOPPED)
117 return true;
118 else
119 return false;
120 }
121
122 /*
123 * Is walreceiver running and streaming (or at least attempting to connect,
124 * or starting up)?
125 */
126 bool
127 WalRcvStreaming(void)
128 {
129 WalRcvData *walrcv = WalRcv;
130 WalRcvState state;
131 pg_time_t startTime;
132
133 SpinLockAcquire(&walrcv->mutex);
134
135 state = walrcv->walRcvState;
136 startTime = walrcv->startTime;
137
138 SpinLockRelease(&walrcv->mutex);
139
140 /*
141 * If it has taken too long for walreceiver to start up, give up. Setting
142 * the state to STOPPED ensures that if walreceiver later does start up
143 * after all, it will see that it's not supposed to be running and die
144 * without doing anything.
145 */
146 if (state == WALRCV_STARTING)
147 {
148 pg_time_t now = (pg_time_t) time(NULL);
149
150 if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
151 {
152 bool stopped = false;
153
154 SpinLockAcquire(&walrcv->mutex);
155 if (walrcv->walRcvState == WALRCV_STARTING)
156 {
157 state = walrcv->walRcvState = WALRCV_STOPPED;
158 stopped = true;
159 }
160 SpinLockRelease(&walrcv->mutex);
161
162 if (stopped)
163 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
164 }
165 }
166
167 if (state == WALRCV_STREAMING || state == WALRCV_STARTING ||
168 state == WALRCV_RESTARTING)
169 return true;
170 else
171 return false;
172 }
173
174 /*
175 * Stop walreceiver (if running) and wait for it to die.
176 * Executed by the Startup process.
177 */
178 void
179 ShutdownWalRcv(void)
180 {
181 WalRcvData *walrcv = WalRcv;
182 pid_t walrcvpid = 0;
183 bool stopped = false;
184
185 /*
186 * Request walreceiver to stop. Walreceiver will switch to WALRCV_STOPPED
187 * mode once it's finished, and will also request postmaster to not
188 * restart itself.
189 */
190 SpinLockAcquire(&walrcv->mutex);
191 switch (walrcv->walRcvState)
192 {
193 case WALRCV_STOPPED:
194 break;
195 case WALRCV_STARTING:
196 walrcv->walRcvState = WALRCV_STOPPED;
197 stopped = true;
198 break;
199
200 case WALRCV_STREAMING:
201 case WALRCV_WAITING:
202 case WALRCV_RESTARTING:
203 walrcv->walRcvState = WALRCV_STOPPING;
204 /* fall through */
205 case WALRCV_STOPPING:
206 walrcvpid = walrcv->pid;
207 break;
208 }
209 SpinLockRelease(&walrcv->mutex);
210
211 /* Unnecessary but consistent. */
212 if (stopped)
213 ConditionVariableBroadcast(&walrcv->walRcvStoppedCV);
214
215 /*
216 * Signal walreceiver process if it was still running.
217 */
218 if (walrcvpid != 0)
219 kill(walrcvpid, SIGTERM);
220
221 /*
222 * Wait for walreceiver to acknowledge its death by setting state to
223 * WALRCV_STOPPED.
224 */
225 ConditionVariablePrepareToSleep(&walrcv->walRcvStoppedCV);
226 while (WalRcvRunning())
227 ConditionVariableSleep(&walrcv->walRcvStoppedCV,
228 WAIT_EVENT_WAL_RECEIVER_EXIT);
229 ConditionVariableCancelSleep();
230 }
231
232 /*
233 * Request postmaster to start walreceiver.
234 *
235 * "recptr" indicates the position where streaming should begin. "conninfo"
236 * is a libpq connection string to use. "slotname" is, optionally, the name
237 * of a replication slot to acquire. "create_temp_slot" indicates to create
238 * a temporary slot when no "slotname" is given.
239 *
240 * WAL receivers do not directly load GUC parameters used for the connection
241 * to the primary, and rely on the values passed down by the caller of this
242 * routine instead. Hence, the addition of any new parameters should happen
243 * through this code path.
244 */
245 void
246 RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo,
247 const char *slotname, bool create_temp_slot)
248 {
249 WalRcvData *walrcv = WalRcv;
250 bool launch = false;
251 pg_time_t now = (pg_time_t) time(NULL);
252 ProcNumber walrcv_proc;
253
254 /*
255 * We always start at the beginning of the segment. That prevents a broken
256 * segment (i.e., with no records in the first half of a segment) from
257 * being created by XLOG streaming, which might cause trouble later on if
258 * the segment is e.g archived.
259 */
260 if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
261 recptr -= XLogSegmentOffset(recptr, wal_segment_size);
262
263 SpinLockAcquire(&walrcv->mutex);
264
265 /* It better be stopped if we try to restart it */
266 Assert(walrcv->walRcvState == WALRCV_STOPPED ||
267 walrcv->walRcvState == WALRCV_WAITING);
268
269 /*
270 * Use configured replication slot if present, and ignore the value of
271 * create_temp_slot as the slot name should be persistent. Otherwise, use
272 * create_temp_slot to determine whether this WAL receiver should create a
273 * temporary slot by itself and use it, or not.
274 */
275 if (slotname != NULL && slotname[0] != '\0')
276 {
277 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
278 walrcv->is_temp_slot = false;
279 }
280 else
281 {
282 walrcv->slotname[0] = '\0';
283 walrcv->is_temp_slot = create_temp_slot;
284 }
285
286 /*
287 * While waiting for instructions, the WAL receiver uses the same
288 * connection, so do not clobber the user-visible conninfo already saved.
289 */
290 if (walrcv->walRcvState == WALRCV_STOPPED)
291 {
292 launch = true;
293 walrcv->walRcvState = WALRCV_STARTING;
294
295 if (conninfo != NULL)
296 strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO);
297 else
298 walrcv->conninfo[0] = '\0';
299 }
300 else
301 walrcv->walRcvState = WALRCV_RESTARTING;
302 walrcv->startTime = now;
303
304 /*
305 * If this is the first startup of walreceiver (on this timeline),
306 * initialize flushedUpto and latestChunkStart to the starting point.
307 */
308 if (walrcv->receiveStart == 0 || walrcv->receivedTLI != tli)
309 {
310 walrcv->flushedUpto = recptr;
311 walrcv->receivedTLI = tli;
312 walrcv->latestChunkStart = recptr;
313 }
314 walrcv->receiveStart = recptr;
315 walrcv->receiveStartTLI = tli;
316
317 walrcv_proc = walrcv->procno;
318
319 SpinLockRelease(&walrcv->mutex);
320
321 if (launch)
322 SendPostmasterSignal(PMSIGNAL_START_WALRECEIVER);
323 else if (walrcv_proc != INVALID_PROC_NUMBER)
324 SetLatch(&GetPGProcByNumber(walrcv_proc)->procLatch);
325 }
326
327 /*
328 * Returns the last+1 byte position that walreceiver has flushed.
329 *
330 * Optionally, returns the previous chunk start, that is the first byte
331 * written in the most recent walreceiver flush cycle. Callers not
332 * interested in that value may pass NULL for latestChunkStart. Same for
333 * receiveTLI.
334 */
335 XLogRecPtr
336 GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
337 {
338 WalRcvData *walrcv = WalRcv;
339 XLogRecPtr recptr;
340
341 SpinLockAcquire(&walrcv->mutex);
342 recptr = walrcv->flushedUpto;
343 if (latestChunkStart)
344 *latestChunkStart = walrcv->latestChunkStart;
345 if (receiveTLI)
346 *receiveTLI = walrcv->receivedTLI;
347 SpinLockRelease(&walrcv->mutex);
348
349 return recptr;
350 }
351
352 /*
353 * Returns the last+1 byte position that walreceiver has written.
354 * This returns a recently written value without taking a lock.
355 */
356 XLogRecPtr
357 GetWalRcvWriteRecPtr(void)
358 {
359 WalRcvData *walrcv = WalRcv;
360
361 return pg_atomic_read_u64(&walrcv->writtenUpto);
362 }
363
364 /*
365 * Returns the replication apply delay in ms or -1
366 * if the apply delay info is not available
367 */
368 int
369 GetReplicationApplyDelay(void)
370 {
371 WalRcvData *walrcv = WalRcv;
372 XLogRecPtr receivePtr;
373 XLogRecPtr replayPtr;
374 TimestampTz chunkReplayStartTime;
375
376 SpinLockAcquire(&walrcv->mutex);
377 receivePtr = walrcv->flushedUpto;
378 SpinLockRelease(&walrcv->mutex);
379
380 replayPtr = GetXLogReplayRecPtr(NULL);
381
382 if (receivePtr == replayPtr)
383 return 0;
384
385 chunkReplayStartTime = GetCurrentChunkReplayStartTime();
386
387 if (chunkReplayStartTime == 0)
388 return -1;
389
390 return TimestampDifferenceMilliseconds(chunkReplayStartTime,
391 GetCurrentTimestamp());
392 }
393
394 /*
395 * Returns the network latency in ms, note that this includes any
396 * difference in clock settings between the servers, as well as timezone.
397 */
398 int
399 GetReplicationTransferLatency(void)
400 {
401 WalRcvData *walrcv = WalRcv;
402 TimestampTz lastMsgSendTime;
403 TimestampTz lastMsgReceiptTime;
404
405 SpinLockAcquire(&walrcv->mutex);
406 lastMsgSendTime = walrcv->lastMsgSendTime;
407 lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
408 SpinLockRelease(&walrcv->mutex);
409
410 return TimestampDifferenceMilliseconds(lastMsgSendTime,
411 lastMsgReceiptTime);
412 }