PostgreSQL Source Code git master
walreceiverfuncs.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "utils/timestamp.h"
Include dependency graph for walreceiverfuncs.c:

Go to the source code of this file.

Macros

#define WALRCV_STARTUP_TIMEOUT   10
 

Functions

Size WalRcvShmemSize (void)
 
void WalRcvShmemInit (void)
 
bool WalRcvRunning (void)
 
WalRcvState WalRcvGetState (void)
 
bool WalRcvStreaming (void)
 
void ShutdownWalRcv (void)
 
void RequestXLogStreaming (TimeLineID tli, XLogRecPtr recptr, const char *conninfo, const char *slotname, bool create_temp_slot)
 
XLogRecPtr GetWalRcvFlushRecPtr (XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
 
XLogRecPtr GetWalRcvWriteRecPtr (void)
 
int GetReplicationApplyDelay (void)
 
int GetReplicationTransferLatency (void)
 

Variables

WalRcvDataWalRcv = NULL
 

Macro Definition Documentation

◆ WALRCV_STARTUP_TIMEOUT

#define WALRCV_STARTUP_TIMEOUT   10

Definition at line 40 of file walreceiverfuncs.c.

Function Documentation

◆ GetReplicationApplyDelay()

int GetReplicationApplyDelay ( void  )

Definition at line 379 of file walreceiverfuncs.c.

380{
381 WalRcvData *walrcv = WalRcv;
382 XLogRecPtr receivePtr;
383 XLogRecPtr replayPtr;
384 TimestampTz chunkReplayStartTime;
385
386 SpinLockAcquire(&walrcv->mutex);
387 receivePtr = walrcv->flushedUpto;
388 SpinLockRelease(&walrcv->mutex);
389
390 replayPtr = GetXLogReplayRecPtr(NULL);
391
392 if (receivePtr == replayPtr)
393 return 0;
394
395 chunkReplayStartTime = GetCurrentChunkReplayStartTime();
396
397 if (chunkReplayStartTime == 0)
398 return -1;
399
400 return TimestampDifferenceMilliseconds(chunkReplayStartTime,
402}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1757
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
int64 TimestampTz
Definition: timestamp.h:39
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
XLogRecPtr flushedUpto
Definition: walreceiver.h:96
slock_t mutex
Definition: walreceiver.h:147
WalRcvData * WalRcv
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TimestampTz GetCurrentChunkReplayStartTime(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References WalRcvData::flushedUpto, GetCurrentChunkReplayStartTime(), GetCurrentTimestamp(), GetXLogReplayRecPtr(), WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, TimestampDifferenceMilliseconds(), and WalRcv.

Referenced by ProcessWalSndrMessage().

◆ GetReplicationTransferLatency()

int GetReplicationTransferLatency ( void  )

Definition at line 409 of file walreceiverfuncs.c.

410{
411 WalRcvData *walrcv = WalRcv;
412 TimestampTz lastMsgSendTime;
413 TimestampTz lastMsgReceiptTime;
414
415 SpinLockAcquire(&walrcv->mutex);
416 lastMsgSendTime = walrcv->lastMsgSendTime;
417 lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
418 SpinLockRelease(&walrcv->mutex);
419
420 return TimestampDifferenceMilliseconds(lastMsgSendTime,
421 lastMsgReceiptTime);
422}
TimestampTz lastMsgReceiptTime
Definition: walreceiver.h:111
TimestampTz lastMsgSendTime
Definition: walreceiver.h:110

References WalRcvData::lastMsgReceiptTime, WalRcvData::lastMsgSendTime, WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, TimestampDifferenceMilliseconds(), and WalRcv.

Referenced by ProcessWalSndrMessage().

◆ GetWalRcvFlushRecPtr()

XLogRecPtr GetWalRcvFlushRecPtr ( XLogRecPtr latestChunkStart,
TimeLineID receiveTLI 
)

Definition at line 346 of file walreceiverfuncs.c.

347{
348 WalRcvData *walrcv = WalRcv;
349 XLogRecPtr recptr;
350
351 SpinLockAcquire(&walrcv->mutex);
352 recptr = walrcv->flushedUpto;
353 if (latestChunkStart)
354 *latestChunkStart = walrcv->latestChunkStart;
355 if (receiveTLI)
356 *receiveTLI = walrcv->receivedTLI;
357 SpinLockRelease(&walrcv->mutex);
358
359 return recptr;
360}
TimeLineID receivedTLI
Definition: walreceiver.h:97
XLogRecPtr latestChunkStart
Definition: walreceiver.h:105
static TimeLineID receiveTLI
Definition: xlogrecovery.c:266

References WalRcvData::flushedUpto, WalRcvData::latestChunkStart, WalRcvData::mutex, WalRcvData::receivedTLI, receiveTLI, SpinLockAcquire, SpinLockRelease, and WalRcv.

Referenced by CreateRestartPoint(), GetLatestLSN(), GetStandbyFlushRecPtr(), pg_last_wal_receive_lsn(), reserve_wal_for_local_slot(), and WaitForWALToBecomeAvailable().

◆ GetWalRcvWriteRecPtr()

XLogRecPtr GetWalRcvWriteRecPtr ( void  )

Definition at line 367 of file walreceiverfuncs.c.

368{
369 WalRcvData *walrcv = WalRcv;
370
371 return pg_atomic_read_u64(&walrcv->writtenUpto);
372}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:465
pg_atomic_uint64 writtenUpto
Definition: walreceiver.h:155

References pg_atomic_read_u64(), WalRcv, and WalRcvData::writtenUpto.

◆ RequestXLogStreaming()

void RequestXLogStreaming ( TimeLineID  tli,
XLogRecPtr  recptr,
const char *  conninfo,
const char *  slotname,
bool  create_temp_slot 
)

Definition at line 260 of file walreceiverfuncs.c.

262{
263 WalRcvData *walrcv = WalRcv;
264 bool launch = false;
265 pg_time_t now = (pg_time_t) time(NULL);
266 ProcNumber walrcv_proc;
267
268 /*
269 * We always start at the beginning of the segment. That prevents a broken
270 * segment (i.e., with no records in the first half of a segment) from
271 * being created by XLOG streaming, which might cause trouble later on if
272 * the segment is e.g archived.
273 */
274 if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
275 recptr -= XLogSegmentOffset(recptr, wal_segment_size);
276
277 SpinLockAcquire(&walrcv->mutex);
278
279 /* It better be stopped if we try to restart it */
280 Assert(walrcv->walRcvState == WALRCV_STOPPED ||
281 walrcv->walRcvState == WALRCV_WAITING);
282
283 if (conninfo != NULL)
284 strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO);
285 else
286 walrcv->conninfo[0] = '\0';
287
288 /*
289 * Use configured replication slot if present, and ignore the value of
290 * create_temp_slot as the slot name should be persistent. Otherwise, use
291 * create_temp_slot to determine whether this WAL receiver should create a
292 * temporary slot by itself and use it, or not.
293 */
294 if (slotname != NULL && slotname[0] != '\0')
295 {
296 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
297 walrcv->is_temp_slot = false;
298 }
299 else
300 {
301 walrcv->slotname[0] = '\0';
302 walrcv->is_temp_slot = create_temp_slot;
303 }
304
305 if (walrcv->walRcvState == WALRCV_STOPPED)
306 {
307 launch = true;
309 }
310 else
312 walrcv->startTime = now;
313
314 /*
315 * If this is the first startup of walreceiver (on this timeline),
316 * initialize flushedUpto and latestChunkStart to the starting point.
317 */
318 if (!XLogRecPtrIsValid(walrcv->receiveStart) || walrcv->receivedTLI != tli)
319 {
320 walrcv->flushedUpto = recptr;
321 walrcv->receivedTLI = tli;
322 walrcv->latestChunkStart = recptr;
323 }
324 walrcv->receiveStart = recptr;
325 walrcv->receiveStartTLI = tli;
326
327 walrcv_proc = walrcv->procno;
328
329 SpinLockRelease(&walrcv->mutex);
330
331 if (launch)
333 else if (walrcv_proc != INVALID_PROC_NUMBER)
334 SetLatch(&GetPGProcByNumber(walrcv_proc)->procLatch);
335}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
Assert(PointerIsAligned(start, uint64))
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:165
@ PMSIGNAL_START_WALRECEIVER
Definition: pmsignal.h:42
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define GetPGProcByNumber(n)
Definition: proc.h:440
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
TimeLineID receiveStartTLI
Definition: walreceiver.h:87
char slotname[NAMEDATALEN]
Definition: walreceiver.h:136
XLogRecPtr receiveStart
Definition: walreceiver.h:86
ProcNumber procno
Definition: walreceiver.h:67
bool is_temp_slot
Definition: walreceiver.h:142
pg_time_t startTime
Definition: walreceiver.h:78
WalRcvState walRcvState
Definition: walreceiver.h:71
char conninfo[MAXCONNINFO]
Definition: walreceiver.h:123
#define MAXCONNINFO
Definition: walreceiver.h:37
@ WALRCV_STARTING
Definition: walreceiver.h:48
@ WALRCV_STOPPED
Definition: walreceiver.h:47
@ WALRCV_RESTARTING
Definition: walreceiver.h:52
@ WALRCV_WAITING
Definition: walreceiver.h:51
int wal_segment_size
Definition: xlog.c:145
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29

References Assert(), WalRcvData::conninfo, WalRcvData::flushedUpto, GetPGProcByNumber, INVALID_PROC_NUMBER, WalRcvData::is_temp_slot, WalRcvData::latestChunkStart, MAXCONNINFO, WalRcvData::mutex, NAMEDATALEN, now(), PMSIGNAL_START_WALRECEIVER, WalRcvData::procno, WalRcvData::receivedTLI, WalRcvData::receiveStart, WalRcvData::receiveStartTLI, SendPostmasterSignal(), SetLatch(), WalRcvData::slotname, SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, strlcpy(), wal_segment_size, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STOPPED, WALRCV_WAITING, WalRcvData::walRcvState, XLogRecPtrIsValid, and XLogSegmentOffset.

Referenced by WaitForWALToBecomeAvailable().

◆ ShutdownWalRcv()

void ShutdownWalRcv ( void  )

Definition at line 193 of file walreceiverfuncs.c.

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
214 case WALRCV_STREAMING:
215 case WALRCV_WAITING:
218 /* fall through */
219 case WALRCV_STOPPING:
220 walrcvpid = walrcv->pid;
221 break;
222 }
223 SpinLockRelease(&walrcv->mutex);
224
225 /* Unnecessary but consistent. */
226 if (stopped)
228
229 /*
230 * Signal walreceiver process if it was still running.
231 */
232 if (walrcvpid != 0)
233 kill(walrcvpid, SIGTERM);
234
235 /*
236 * Wait for walreceiver to acknowledge its death by setting state to
237 * WALRCV_STOPPED.
238 */
240 while (WalRcvRunning())
242 WAIT_EVENT_WAL_RECEIVER_EXIT);
244}
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
pid_t pid
Definition: walreceiver.h:68
ConditionVariable walRcvStoppedCV
Definition: walreceiver.h:72
@ WALRCV_STREAMING
Definition: walreceiver.h:50
@ WALRCV_STOPPING
Definition: walreceiver.h:53
bool WalRcvRunning(void)
#define kill(pid, sig)
Definition: win32_port.h:493

References ConditionVariableBroadcast(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), kill, WalRcvData::mutex, WalRcvData::pid, SpinLockAcquire, SpinLockRelease, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STOPPED, WALRCV_STOPPING, WALRCV_STREAMING, WALRCV_WAITING, WalRcvRunning(), WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by XLogShutdownWalRcv().

◆ WalRcvGetState()

WalRcvState WalRcvGetState ( void  )

Definition at line 124 of file walreceiverfuncs.c.

125{
126 WalRcvData *walrcv = WalRcv;
128
129 SpinLockAcquire(&walrcv->mutex);
130 state = walrcv->walRcvState;
131 SpinLockRelease(&walrcv->mutex);
132
133 return state;
134}
Definition: regguts.h:323
WalRcvState
Definition: walreceiver.h:46

References WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, WalRcv, and WalRcvData::walRcvState.

Referenced by WaitForWALToBecomeAvailable().

◆ WalRcvRunning()

bool WalRcvRunning ( void  )

Definition at line 76 of file walreceiverfuncs.c.

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}
#define WALRCV_STARTUP_TIMEOUT

References ConditionVariableBroadcast(), WalRcvData::mutex, now(), SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, WalRcv, WALRCV_STARTING, WALRCV_STARTUP_TIMEOUT, WALRCV_STOPPED, WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by ShutdownWalRcv(), and StartupRequestWalReceiverRestart().

◆ WalRcvShmemInit()

void WalRcvShmemInit ( void  )

Definition at line 55 of file walreceiverfuncs.c.

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 */
71 }
72}
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:451
#define MemSet(start, val, len)
Definition: c.h:1022
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:389
#define SpinLockInit(lock)
Definition: spin.h:57
Size WalRcvShmemSize(void)

References ConditionVariableInit(), INVALID_PROC_NUMBER, MemSet, WalRcvData::mutex, pg_atomic_init_u64(), WalRcvData::procno, ShmemInitStruct(), SpinLockInit, WalRcv, WALRCV_STOPPED, WalRcvShmemSize(), WalRcvData::walRcvState, WalRcvData::walRcvStoppedCV, and WalRcvData::writtenUpto.

Referenced by CreateOrAttachShmemStructs().

◆ WalRcvShmemSize()

Size WalRcvShmemSize ( void  )

Definition at line 44 of file walreceiverfuncs.c.

45{
46 Size size = 0;
47
48 size = add_size(size, sizeof(WalRcvData));
49
50 return size;
51}
size_t Size
Definition: c.h:613
Size add_size(Size s1, Size s2)
Definition: shmem.c:495

References add_size().

Referenced by CalculateShmemSize(), and WalRcvShmemInit().

◆ WalRcvStreaming()

bool WalRcvStreaming ( void  )

Definition at line 141 of file walreceiverfuncs.c.

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}

References ConditionVariableBroadcast(), WalRcvData::mutex, now(), SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STARTUP_TIMEOUT, WALRCV_STOPPED, WALRCV_STREAMING, WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by FinishWalRecovery(), and WaitForWALToBecomeAvailable().

Variable Documentation

◆ WalRcv