PostgreSQL Source Code git master
xlogwait.h File Reference
#include "access/xlogdefs.h"
#include "lib/pairingheap.h"
#include "port/atomics.h"
#include "storage/procnumber.h"
#include "storage/spin.h"
#include "tcop/dest.h"
Include dependency graph for xlogwait.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  WaitLSNProcInfo
 
struct  WaitLSNState
 

Typedefs

typedef enum WaitLSNType WaitLSNType
 
typedef struct WaitLSNProcInfo WaitLSNProcInfo
 
typedef struct WaitLSNState WaitLSNState
 

Enumerations

enum  WaitLSNResult { WAIT_LSN_RESULT_SUCCESS , WAIT_LSN_RESULT_NOT_IN_RECOVERY , WAIT_LSN_RESULT_TIMEOUT }
 
enum  WaitLSNType { WAIT_LSN_TYPE_REPLAY = 0 , WAIT_LSN_TYPE_FLUSH = 1 , WAIT_LSN_TYPE_COUNT = 2 }
 

Functions

Size WaitLSNShmemSize (void)
 
void WaitLSNShmemInit (void)
 
void WaitLSNWakeup (WaitLSNType lsnType, XLogRecPtr currentLSN)
 
void WaitLSNCleanup (void)
 
WaitLSNResult WaitForLSN (WaitLSNType lsnType, XLogRecPtr targetLSN, int64 timeout)
 

Variables

PGDLLIMPORT WaitLSNStatewaitLSNState
 

Typedef Documentation

◆ WaitLSNProcInfo

◆ WaitLSNState

typedef struct WaitLSNState WaitLSNState

◆ WaitLSNType

typedef enum WaitLSNType WaitLSNType

Enumeration Type Documentation

◆ WaitLSNResult

Enumerator
WAIT_LSN_RESULT_SUCCESS 
WAIT_LSN_RESULT_NOT_IN_RECOVERY 
WAIT_LSN_RESULT_TIMEOUT 

Definition at line 25 of file xlogwait.h.

26{
27 WAIT_LSN_RESULT_SUCCESS, /* Target LSN is reached */
28 WAIT_LSN_RESULT_NOT_IN_RECOVERY, /* Recovery ended before or during our
29 * wait */
30 WAIT_LSN_RESULT_TIMEOUT /* Timeout occurred */
WaitLSNResult
Definition: xlogwait.h:26
@ WAIT_LSN_RESULT_NOT_IN_RECOVERY
Definition: xlogwait.h:28
@ WAIT_LSN_RESULT_TIMEOUT
Definition: xlogwait.h:30
@ WAIT_LSN_RESULT_SUCCESS
Definition: xlogwait.h:27

◆ WaitLSNType

Enumerator
WAIT_LSN_TYPE_REPLAY 
WAIT_LSN_TYPE_FLUSH 
WAIT_LSN_TYPE_COUNT 

Definition at line 36 of file xlogwait.h.

37{
38 WAIT_LSN_TYPE_REPLAY = 0, /* Waiting for replay on standby */
39 WAIT_LSN_TYPE_FLUSH = 1, /* Waiting for flush on primary */
WaitLSNType
Definition: xlogwait.h:37
@ WAIT_LSN_TYPE_COUNT
Definition: xlogwait.h:40
@ WAIT_LSN_TYPE_FLUSH
Definition: xlogwait.h:39
@ WAIT_LSN_TYPE_REPLAY
Definition: xlogwait.h:38

Function Documentation

◆ WaitForLSN()

WaitLSNResult WaitForLSN ( WaitLSNType  lsnType,
XLogRecPtr  targetLSN,
int64  timeout 
)

Definition at line 314 of file xlogwait.c.

315{
316 XLogRecPtr currentLSN;
317 TimestampTz endtime = 0;
318 int wake_events = WL_LATCH_SET | WL_POSTMASTER_DEATH;
319
320 /* Shouldn't be called when shmem isn't initialized */
322
323 /* Should have a valid proc number */
325
326 if (timeout > 0)
327 {
329 wake_events |= WL_TIMEOUT;
330 }
331
332 /*
333 * Add our process to the waiters heap. It might happen that target LSN
334 * gets reached before we do. The check at the beginning of the loop
335 * below prevents the race condition.
336 */
337 addLSNWaiter(targetLSN, lsnType);
338
339 for (;;)
340 {
341 int rc;
342 long delay_ms = -1;
343
344 if (lsnType == WAIT_LSN_TYPE_REPLAY)
345 currentLSN = GetXLogReplayRecPtr(NULL);
346 else
347 currentLSN = GetFlushRecPtr(NULL);
348
349 /* Check that recovery is still in-progress */
350 if (lsnType == WAIT_LSN_TYPE_REPLAY && !RecoveryInProgress())
351 {
352 /*
353 * Recovery was ended, but check if target LSN was already
354 * reached.
355 */
356 deleteLSNWaiter(lsnType);
357
358 if (PromoteIsTriggered() && targetLSN <= currentLSN)
361 }
362 else
363 {
364 /* Check if the waited LSN has been reached */
365 if (targetLSN <= currentLSN)
366 break;
367 }
368
369 if (timeout > 0)
370 {
372 if (delay_ms <= 0)
373 break;
374 }
375
377
378 rc = WaitLatch(MyLatch, wake_events, delay_ms,
379 (lsnType == WAIT_LSN_TYPE_REPLAY) ? WAIT_EVENT_WAIT_FOR_WAL_REPLAY : WAIT_EVENT_WAIT_FOR_WAL_FLUSH);
380
381 /*
382 * Emergency bailout if postmaster has died. This is to avoid the
383 * necessity for manual cleanup of all postmaster children.
384 */
385 if (rc & WL_POSTMASTER_DEATH)
387 errcode(ERRCODE_ADMIN_SHUTDOWN),
388 errmsg("terminating connection due to unexpected postmaster exit"),
389 errcontext("while waiting for LSN"));
390
391 if (rc & WL_LATCH_SET)
393 }
394
395 /*
396 * Delete our process from the shared memory heap. We might already be
397 * deleted by the startup process. The 'inHeap' flags prevents us from
398 * the double deletion.
399 */
400 deleteLSNWaiter(lsnType);
401
402 /*
403 * If we didn't reach the target LSN, we must be exited by timeout.
404 */
405 if (targetLSN > currentLSN)
407
409}
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
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define errcontext
Definition: elog.h:198
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:150
ProcNumber MyProcNumber
Definition: globals.c:90
int MaxBackends
Definition: globals.c:146
struct Latch * MyLatch
Definition: globals.c:63
Assert(PointerIsAligned(start, uint64))
void ResetLatch(Latch *latch)
Definition: latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_LATCH_SET
Definition: waiteventset.h:34
#define WL_POSTMASTER_DEATH
Definition: waiteventset.h:38
bool RecoveryInProgress(void)
Definition: xlog.c:6406
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6571
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool PromoteIsTriggered(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
struct WaitLSNState * waitLSNState
Definition: xlogwait.c:63
static void deleteLSNWaiter(WaitLSNType lsnType)
Definition: xlogwait.c:170
static void addLSNWaiter(XLogRecPtr lsn, WaitLSNType lsnType)
Definition: xlogwait.c:145

References addLSNWaiter(), Assert(), CHECK_FOR_INTERRUPTS, deleteLSNWaiter(), ereport, errcode(), errcontext, errmsg(), FATAL, GetCurrentTimestamp(), GetFlushRecPtr(), GetXLogReplayRecPtr(), MaxBackends, MyLatch, MyProcNumber, PromoteIsTriggered(), RecoveryInProgress(), ResetLatch(), TimestampDifferenceMilliseconds(), TimestampTzPlusMilliseconds, WAIT_LSN_RESULT_NOT_IN_RECOVERY, WAIT_LSN_RESULT_SUCCESS, WAIT_LSN_RESULT_TIMEOUT, WAIT_LSN_TYPE_REPLAY, WaitLatch(), waitLSNState, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by ExecWaitStmt().

◆ WaitLSNCleanup()

void WaitLSNCleanup ( void  )

Definition at line 290 of file xlogwait.c.

291{
292 if (waitLSNState)
293 {
294 /*
295 * We do a fast-path check of the inHeap flag without the lock. This
296 * flag is set to true only by the process itself. So, it's only
297 * possible to get a false positive. But that will be eliminated by a
298 * recheck inside deleteLSNWaiter().
299 */
302 }
303}
WaitLSNType lsnType
Definition: xlogwait.h:54
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER]
Definition: xlogwait.h:91

References deleteLSNWaiter(), WaitLSNProcInfo::inHeap, WaitLSNProcInfo::lsnType, MyProcNumber, WaitLSNState::procInfos, and waitLSNState.

Referenced by AbortTransaction(), and ProcKill().

◆ WaitLSNShmemInit()

void WaitLSNShmemInit ( void  )

Definition at line 78 of file xlogwait.c.

79{
80 bool found;
81
82 waitLSNState = (WaitLSNState *) ShmemInitStruct("WaitLSNState",
84 &found);
85 if (!found)
86 {
87 int i;
88
89 /* Initialize heaps and tracking */
90 for (i = 0; i < WAIT_LSN_TYPE_COUNT; i++)
91 {
94 }
95
96 /* Initialize process info array */
97 memset(&waitLSNState->procInfos, 0,
99 }
100}
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:451
#define PG_UINT64_MAX
Definition: c.h:603
int i
Definition: isn.c:77
void pairingheap_initialize(pairingheap *heap, pairingheap_comparator compare, void *arg)
Definition: pairingheap.c:60
#define NUM_AUXILIARY_PROCS
Definition: proc.h:463
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
pg_atomic_uint64 minWaitedLSN[WAIT_LSN_TYPE_COUNT]
Definition: xlogwait.h:79
pairingheap waitersHeap[WAIT_LSN_TYPE_COUNT]
Definition: xlogwait.h:85
static int waitlsn_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
Definition: xlogwait.c:107
Size WaitLSNShmemSize(void)
Definition: xlogwait.c:67

References i, MaxBackends, WaitLSNState::minWaitedLSN, NUM_AUXILIARY_PROCS, pairingheap_initialize(), pg_atomic_init_u64(), PG_UINT64_MAX, WaitLSNState::procInfos, ShmemInitStruct(), WAIT_LSN_TYPE_COUNT, WaitLSNState::waitersHeap, waitlsn_cmp(), WaitLSNShmemSize(), and waitLSNState.

Referenced by CreateOrAttachShmemStructs().

◆ WaitLSNShmemSize()

Size WaitLSNShmemSize ( void  )

Definition at line 67 of file xlogwait.c.

68{
69 Size size;
70
71 size = offsetof(WaitLSNState, procInfos);
73 return size;
74}
size_t Size
Definition: c.h:615
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511

References add_size(), MaxBackends, mul_size(), NUM_AUXILIARY_PROCS, and WaitLSNState::procInfos.

Referenced by CalculateShmemSize(), and WaitLSNShmemInit().

◆ WaitLSNWakeup()

void WaitLSNWakeup ( WaitLSNType  lsnType,
XLogRecPtr  currentLSN 
)

Definition at line 269 of file xlogwait.c.

270{
271 int i = (int) lsnType;
272
273 Assert(i >= 0 && i < (int) WAIT_LSN_TYPE_COUNT);
274
275 /*
276 * Fast path check. Skip if currentLSN is InvalidXLogRecPtr, which means
277 * "wake all waiters" (e.g., during promotion when recovery ends).
278 */
279 if (XLogRecPtrIsValid(currentLSN) &&
281 return;
282
283 wakeupWaiters(lsnType, currentLSN);
284}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:465
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29
static void wakeupWaiters(WaitLSNType lsnType, XLogRecPtr currentLSN)
Definition: xlogwait.c:210

References Assert(), i, WaitLSNState::minWaitedLSN, pg_atomic_read_u64(), WAIT_LSN_TYPE_COUNT, waitLSNState, wakeupWaiters(), and XLogRecPtrIsValid.

Referenced by PerformWalRecovery(), and StartupXLOG().

Variable Documentation

◆ waitLSNState