PostgreSQL Source Code git master
Loading...
Searching...
No Matches
walreceiverfuncs.c
Go to the documentation of this file.
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
26#include "access/xlogrecovery.h"
27#include "pgstat.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
37
38static void WalRcvShmemRequest(void *arg);
39static void WalRcvShmemInit(void *arg);
40
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 */
53static void
55{
56 ShmemRequestStruct(.name = "Wal Receiver Ctl",
57 .size = sizeof(WalRcvData),
58 .ptr = (void **) &WalRcv,
59 );
60}
61
62/* Initialize walreceiver-related shared memory */
63static void
73
74/* Is walreceiver running (or starting up)? */
75bool
77{
78 WalRcvData *walrcv = WalRcv;
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 */
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)
113 }
114 }
115
116 if (state != WALRCV_STOPPED)
117 return true;
118 else
119 return false;
120}
121
122/* Return the state of the walreceiver. */
125{
126 WalRcvData *walrcv = WalRcv;
128
129 SpinLockAcquire(&walrcv->mutex);
130 state = walrcv->walRcvState;
131 SpinLockRelease(&walrcv->mutex);
132
133 return state;
134}
135
136/*
137 * Is walreceiver running and streaming (or at least attempting to connect,
138 * or starting up)?
139 */
140bool
142{
143 WalRcvData *walrcv = WalRcv;
145 pg_time_t startTime;
146
147 SpinLockAcquire(&walrcv->mutex);
148
149 state = walrcv->walRcvState;
150 startTime = walrcv->startTime;
151
152 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 if (state == WALRCV_STARTING)
161 {
162 pg_time_t now = (pg_time_t) time(NULL);
163
164 if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
165 {
166 bool stopped = false;
167
168 SpinLockAcquire(&walrcv->mutex);
169 if (walrcv->walRcvState == WALRCV_STARTING)
170 {
171 state = walrcv->walRcvState = WALRCV_STOPPED;
172 stopped = true;
173 }
174 SpinLockRelease(&walrcv->mutex);
175
176 if (stopped)
178 }
179 }
180
183 return true;
184 else
185 return false;
186}
187
188/*
189 * Stop walreceiver (if running) and wait for it to die.
190 * Executed by the Startup process.
191 */
192void
194{
195 WalRcvData *walrcv = WalRcv;
196 pid_t walrcvpid = 0;
197 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 */
204 SpinLockAcquire(&walrcv->mutex);
205 switch (walrcv->walRcvState)
206 {
207 case WALRCV_STOPPED:
208 break;
209 case WALRCV_STARTING:
210 walrcv->walRcvState = WALRCV_STOPPED;
211 stopped = true;
212 break;
213
215 case WALRCV_STREAMING:
216 case WALRCV_WAITING:
220 case WALRCV_STOPPING:
221 walrcvpid = walrcv->pid;
222 break;
223 }
224 SpinLockRelease(&walrcv->mutex);
225
226 /* Unnecessary but consistent. */
227 if (stopped)
229
230 /*
231 * Signal walreceiver process if it was still running.
232 */
233 if (walrcvpid != 0)
234 kill(walrcvpid, SIGTERM);
235
236 /*
237 * Wait for walreceiver to acknowledge its death by setting state to
238 * WALRCV_STOPPED.
239 */
241 while (WalRcvRunning())
243 WAIT_EVENT_WAL_RECEIVER_EXIT);
245}
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 */
260void
261RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo,
262 const char *slotname, bool create_temp_slot)
263{
264 WalRcvData *walrcv = WalRcv;
265 bool launch = false;
266 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 */
275 if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
276 recptr -= XLogSegmentOffset(recptr, wal_segment_size);
277
278 SpinLockAcquire(&walrcv->mutex);
279
280 /* It better be stopped if we try to restart it */
281 Assert(walrcv->walRcvState == WALRCV_STOPPED ||
282 walrcv->walRcvState == WALRCV_WAITING);
283
284 if (conninfo != NULL)
285 strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO);
286 else
287 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 */
295 if (slotname != NULL && slotname[0] != '\0')
296 {
297 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
298 walrcv->is_temp_slot = false;
299 }
300 else
301 {
302 walrcv->slotname[0] = '\0';
303 walrcv->is_temp_slot = create_temp_slot;
304 }
305
306 if (walrcv->walRcvState == WALRCV_STOPPED)
307 {
308 launch = true;
310 }
311 else
313 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 */
319 if (!XLogRecPtrIsValid(walrcv->receiveStart) || walrcv->receivedTLI != tli)
320 {
321 walrcv->flushedUpto = recptr;
322 walrcv->receivedTLI = tli;
323 walrcv->latestChunkStart = recptr;
324 pg_atomic_write_u64(&walrcv->writtenUpto, recptr);
325 }
326 walrcv->receiveStart = recptr;
327 walrcv->receiveStartTLI = tli;
328
329 walrcv_proc = walrcv->procno;
330
331 SpinLockRelease(&walrcv->mutex);
332
333 if (launch)
335 else if (walrcv_proc != INVALID_PROC_NUMBER)
336 SetLatch(&GetPGProcByNumber(walrcv_proc)->procLatch);
337}
338
339/*
340 * Returns the last+1 byte position that walreceiver has flushed.
341 *
342 * Optionally, returns the previous chunk start, that is the first byte
343 * written in the most recent walreceiver flush cycle. Callers not
344 * interested in that value may pass NULL for latestChunkStart. Same for
345 * receiveTLI.
346 */
349{
350 WalRcvData *walrcv = WalRcv;
351 XLogRecPtr recptr;
352
353 SpinLockAcquire(&walrcv->mutex);
354 recptr = walrcv->flushedUpto;
355 if (latestChunkStart)
356 *latestChunkStart = walrcv->latestChunkStart;
357 if (receiveTLI)
358 *receiveTLI = walrcv->receivedTLI;
359 SpinLockRelease(&walrcv->mutex);
360
361 return recptr;
362}
363
364/*
365 * Returns the last+1 byte position that walreceiver has written.
366 * This returns a recently written value without taking a lock.
367 */
370{
371 WalRcvData *walrcv = WalRcv;
372
373 return pg_atomic_read_u64(&walrcv->writtenUpto);
374}
375
376/*
377 * Returns the replication apply delay in ms or -1
378 * if the apply delay info is not available
379 */
380int
382{
383 WalRcvData *walrcv = WalRcv;
384 XLogRecPtr receivePtr;
385 XLogRecPtr replayPtr;
386 TimestampTz chunkReplayStartTime;
387
388 SpinLockAcquire(&walrcv->mutex);
389 receivePtr = walrcv->flushedUpto;
390 SpinLockRelease(&walrcv->mutex);
391
392 replayPtr = GetXLogReplayRecPtr(NULL);
393
394 if (receivePtr == replayPtr)
395 return 0;
396
397 chunkReplayStartTime = GetCurrentChunkReplayStartTime();
398
399 if (chunkReplayStartTime == 0)
400 return -1;
401
402 return TimestampDifferenceMilliseconds(chunkReplayStartTime,
404}
405
406/*
407 * Returns the network latency in ms, note that this includes any
408 * difference in clock settings between the servers, as well as timezone.
409 */
410int
412{
413 WalRcvData *walrcv = WalRcv;
414 TimestampTz lastMsgSendTime;
415 TimestampTz lastMsgReceiptTime;
416
417 SpinLockAcquire(&walrcv->mutex);
418 lastMsgSendTime = walrcv->lastMsgSendTime;
419 lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
420 SpinLockRelease(&walrcv->mutex);
421
422 return TimestampDifferenceMilliseconds(lastMsgSendTime,
423 lastMsgReceiptTime);
424}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:453
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1751
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1639
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1603
#define Assert(condition)
Definition c.h:943
#define pg_fallthrough
Definition c.h:161
#define MemSet(start, val, len)
Definition c.h:1107
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableInit(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
int64 TimestampTz
Definition timestamp.h:39
Datum arg
Definition elog.c:1322
void SetLatch(Latch *latch)
Definition latch.c:290
#define NAMEDATALEN
int64 pg_time_t
Definition pgtime.h:23
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:164
@ PMSIGNAL_START_WALRECEIVER
Definition pmsignal.h:43
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
#define GetPGProcByNumber(n)
Definition proc.h:504
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int ProcNumber
Definition procnumber.h:24
#define ShmemRequestStruct(...)
Definition shmem.h:176
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
ShmemRequestCallback request_fn
Definition shmem.h:133
TimestampTz lastMsgReceiptTime
TimeLineID receiveStartTLI
Definition walreceiver.h:88
TimeLineID receivedTLI
Definition walreceiver.h:98
char slotname[NAMEDATALEN]
XLogRecPtr latestChunkStart
XLogRecPtr receiveStart
Definition walreceiver.h:87
XLogRecPtr flushedUpto
Definition walreceiver.h:97
ProcNumber procno
Definition walreceiver.h:68
ConditionVariable walRcvStoppedCV
Definition walreceiver.h:73
bool is_temp_slot
pg_atomic_uint64 writtenUpto
pg_time_t startTime
Definition walreceiver.h:79
TimestampTz lastMsgSendTime
WalRcvState walRcvState
Definition walreceiver.h:72
slock_t mutex
char conninfo[MAXCONNINFO]
const char * name
#define MAXCONNINFO
Definition walreceiver.h:37
WalRcvState
Definition walreceiver.h:46
@ WALRCV_STARTING
Definition walreceiver.h:48
@ WALRCV_STOPPED
Definition walreceiver.h:47
@ WALRCV_CONNECTING
Definition walreceiver.h:50
@ WALRCV_RESTARTING
Definition walreceiver.h:53
@ WALRCV_STREAMING
Definition walreceiver.h:51
@ WALRCV_WAITING
Definition walreceiver.h:52
@ WALRCV_STOPPING
Definition walreceiver.h:54
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool WalRcvStreaming(void)
void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, const char *slotname, bool create_temp_slot)
WalRcvData * WalRcv
XLogRecPtr GetWalRcvWriteRecPtr(void)
void ShutdownWalRcv(void)
const ShmemCallbacks WalRcvShmemCallbacks
#define WALRCV_STARTUP_TIMEOUT
static void WalRcvShmemInit(void *arg)
WalRcvState WalRcvGetState(void)
static void WalRcvShmemRequest(void *arg)
bool WalRcvRunning(void)
int GetReplicationApplyDelay(void)
int GetReplicationTransferLatency(void)
#define kill(pid, sig)
Definition win32_port.h:490
int wal_segment_size
Definition xlog.c:150
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63
static TimeLineID receiveTLI
TimestampTz GetCurrentChunkReplayStartTime(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)