PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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
 

Macros

#define WAIT_LSN_TYPE_COUNT   (WAIT_LSN_TYPE_PRIMARY_FLUSH + 1)
 

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_STANDBY_REPLAY , WAIT_LSN_TYPE_STANDBY_WRITE , WAIT_LSN_TYPE_STANDBY_FLUSH , WAIT_LSN_TYPE_PRIMARY_FLUSH }
 

Functions

XLogRecPtr GetCurrentLSNForWaitType (WaitLSNType lsnType)
 
void WaitLSNWakeup (WaitLSNType lsnType, XLogRecPtr currentLSN)
 
void WaitLSNCleanup (void)
 
WaitLSNResult WaitForLSN (WaitLSNType lsnType, XLogRecPtr targetLSN, int64 timeout)
 

Variables

PGDLLIMPORT WaitLSNStatewaitLSNState
 

Macro Definition Documentation

◆ WAIT_LSN_TYPE_COUNT

#define WAIT_LSN_TYPE_COUNT   (WAIT_LSN_TYPE_PRIMARY_FLUSH + 1)

Definition at line 47 of file xlogwait.h.

Typedef Documentation

◆ WaitLSNProcInfo

◆ WaitLSNState

◆ 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_STANDBY_REPLAY 
WAIT_LSN_TYPE_STANDBY_WRITE 
WAIT_LSN_TYPE_STANDBY_FLUSH 
WAIT_LSN_TYPE_PRIMARY_FLUSH 

Definition at line 36 of file xlogwait.h.

37{
38 /* Standby wait types (walreceiver/startup wakes) */
42
43 /* Primary wait types (WAL writer/backends wake) */
WaitLSNType
Definition xlogwait.h:37
@ WAIT_LSN_TYPE_PRIMARY_FLUSH
Definition xlogwait.h:44
@ WAIT_LSN_TYPE_STANDBY_REPLAY
Definition xlogwait.h:39
@ WAIT_LSN_TYPE_STANDBY_FLUSH
Definition xlogwait.h:41
@ WAIT_LSN_TYPE_STANDBY_WRITE
Definition xlogwait.h:40

Function Documentation

◆ GetCurrentLSNForWaitType()

XLogRecPtr GetCurrentLSNForWaitType ( WaitLSNType  lsnType)
extern

Definition at line 99 of file xlogwait.c.

100{
101 Assert(lsnType >= 0 && lsnType < WAIT_LSN_TYPE_COUNT);
102
103 /*
104 * All of the cases below provide memory barrier semantics:
105 * GetWalRcvWriteRecPtr() and GetFlushRecPtr() have explicit barriers,
106 * while GetXLogReplayRecPtr() and GetWalRcvFlushRecPtr() use spinlocks.
107 */
108 switch (lsnType)
109 {
112
114 {
117
118 /*
119 * Use the replay position as a floor. WAL up to the replay
120 * point is already on disk from a base backup, archive
121 * restore, or prior streaming, so there is no reason to wait
122 * for the walreceiver to re-receive it.
123 */
124 return Max(recptr, replay);
125 }
126
128 {
131
132 /* Same floor as standby_write; see comment above. */
133 return Max(recptr, replay);
134 }
135
137 return GetFlushRecPtr(NULL);
138 }
139
140 elog(ERROR, "invalid LSN wait type: %d", lsnType);
142}
#define Max(x, y)
Definition c.h:1085
#define Assert(condition)
Definition c.h:943
#define pg_unreachable()
Definition c.h:367
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
static int fb(int x)
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
XLogRecPtr GetWalRcvWriteRecPtr(void)
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6997
uint64 XLogRecPtr
Definition xlogdefs.h:21
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
#define WAIT_LSN_TYPE_COUNT
Definition xlogwait.h:47

References Assert, elog, ERROR, fb(), GetFlushRecPtr(), GetWalRcvFlushRecPtr(), GetWalRcvWriteRecPtr(), GetXLogReplayRecPtr(), Max, pg_unreachable, WAIT_LSN_TYPE_COUNT, WAIT_LSN_TYPE_PRIMARY_FLUSH, WAIT_LSN_TYPE_STANDBY_FLUSH, WAIT_LSN_TYPE_STANDBY_REPLAY, and WAIT_LSN_TYPE_STANDBY_WRITE.

Referenced by ExecWaitStmt(), and WaitForLSN().

◆ WaitForLSN()

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

Definition at line 403 of file xlogwait.c.

404{
408
409 /* Shouldn't be called when shmem isn't initialized */
411
412 /* Should have a valid proc number */
414
415 if (timeout > 0)
416 {
419 }
420
421 /*
422 * Add our process to the waiters heap. It might happen that target LSN
423 * gets reached before we do. The check at the beginning of the loop
424 * below prevents the race condition.
425 */
426 addLSNWaiter(targetLSN, lsnType);
427
428 for (;;)
429 {
430 int rc;
431 long delay_ms = -1;
432
433 /* Get current LSN for the wait type */
435
436 /* Check that recovery is still in-progress */
438 {
439 /*
440 * Recovery was ended, but check if target LSN was already
441 * reached.
442 */
443 deleteLSNWaiter(lsnType);
444
448 }
449 else
450 {
451 /* Check if the waited LSN has been reached */
452 if (targetLSN <= currentLSN)
453 break;
454 }
455
456 if (timeout > 0)
457 {
459 if (delay_ms <= 0)
460 break;
461 }
462
464
465 rc = WaitLatch(MyLatch, wake_events, delay_ms,
466 WaitLSNWaitEvents[lsnType]);
467
468 /*
469 * Emergency bailout if postmaster has died. This is to avoid the
470 * necessity for manual cleanup of all postmaster children.
471 */
472 if (rc & WL_POSTMASTER_DEATH)
475 errmsg("terminating connection due to unexpected postmaster exit"),
476 errcontext("while waiting for LSN"));
477
479 }
480
481 /*
482 * Delete our process from the shared memory heap. We might already be
483 * deleted by the startup process. The 'inHeap' flags prevents us from
484 * the double deletion.
485 */
486 deleteLSNWaiter(lsnType);
487
488 /*
489 * If we didn't reach the target LSN, we must be exited by timeout.
490 */
491 if (targetLSN > currentLSN)
493
495}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1765
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1649
int64 TimestampTz
Definition timestamp.h:39
int errcode(int sqlerrcode)
Definition elog.c:875
#define errcontext
Definition elog.h:200
#define FATAL
Definition elog.h:42
#define ereport(elevel,...)
Definition elog.h:152
ProcNumber MyProcNumber
Definition globals.c:92
int MaxBackends
Definition globals.c:149
struct Latch * MyLatch
Definition globals.c:65
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:125
static char * errmsg
#define NUM_AUXILIARY_PROCS
Definition proc.h:527
#define TimestampTzPlusMilliseconds(tz, ms)
Definition timestamp.h:85
#define WL_TIMEOUT
#define WL_LATCH_SET
#define WL_POSTMASTER_DEATH
bool RecoveryInProgress(void)
Definition xlog.c:6832
bool PromoteIsTriggered(void)
static bool WaitLSNTypeRequiresRecovery(WaitLSNType t)
Definition xlogwait.c:387
struct WaitLSNState * waitLSNState
Definition xlogwait.c:70
static void deleteLSNWaiter(WaitLSNType lsnType)
Definition xlogwait.c:243
XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType)
Definition xlogwait.c:99
static void addLSNWaiter(XLogRecPtr lsn, WaitLSNType lsnType)
Definition xlogwait.c:218
static const uint32 WaitLSNWaitEvents[]
Definition xlogwait.c:84

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

Referenced by decode_concurrent_changes(), and ExecWaitStmt().

◆ WaitLSNCleanup()

void WaitLSNCleanup ( void  )
extern

Definition at line 366 of file xlogwait.c.

367{
368 if (waitLSNState)
369 {
370 /*
371 * We do a fast-path check of the inHeap flag without the lock. This
372 * flag is set to true only by the process itself. So, it's only
373 * possible to get a false positive. But that will be eliminated by a
374 * recheck inside deleteLSNWaiter().
375 */
378 }
379}
WaitLSNType lsnType
Definition xlogwait.h:60
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER]
Definition xlogwait.h:97

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

Referenced by AbortSubTransaction(), AbortTransaction(), and ProcKill().

◆ WaitLSNWakeup()

void WaitLSNWakeup ( WaitLSNType  lsnType,
XLogRecPtr  currentLSN 
)
extern

Definition at line 344 of file xlogwait.c.

345{
346 int i = (int) lsnType;
347
348 Assert(i >= 0 && i < WAIT_LSN_TYPE_COUNT);
349
350 /*
351 * Fast path check. Skip if currentLSN is InvalidXLogRecPtr, which means
352 * "wake all waiters" (e.g., during promotion when recovery ends). Pairs
353 * with pg_atomic_write_membarrier_u64() in updateMinWaitedLSN().
354 */
357 return;
358
359 wakeupWaiters(lsnType, currentLSN);
360}
static uint64 pg_atomic_read_membarrier_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:476
int i
Definition isn.c:77
pg_atomic_uint64 minWaitedLSN[WAIT_LSN_TYPE_COUNT]
Definition xlogwait.h:85
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
static void wakeupWaiters(WaitLSNType lsnType, XLogRecPtr currentLSN)
Definition xlogwait.c:283

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

Referenced by PerformWalRecovery(), StartupXLOG(), XLogBackgroundFlush(), XLogFlush(), XLogWalRcvFlush(), and XLogWalRcvWrite().

Variable Documentation

◆ waitLSNState