Skip to content

Commit 723d018

Browse files
committed
Use a latch to make startup process wake up and replay immediately when
new WAL arrives via streaming replication. This reduces the latency, and also allows us to use a longer polling interval, which is good for energy efficiency. We still need to poll to check for the appearance of a trigger file, but the interval is now 5 seconds (instead of 100ms), like when waiting for a new WAL segment to appear in WAL archive.
1 parent 236b6bc commit 723d018

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/backend/access/transam/xlog.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.434 2010/08/30 15:37:41 sriggs Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.435 2010/09/15 10:35:05 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -46,6 +46,7 @@
4646
#include "storage/bufmgr.h"
4747
#include "storage/fd.h"
4848
#include "storage/ipc.h"
49+
#include "storage/latch.h"
4950
#include "storage/pmsignal.h"
5051
#include "storage/procarray.h"
5152
#include "storage/smgr.h"
@@ -392,6 +393,13 @@ typedef struct XLogCtlData
392393
*/
393394
bool SharedRecoveryInProgress;
394395

396+
/*
397+
* recoveryWakeupLatch is used to wake up the startup process to
398+
* continue WAL replay, if it is waiting for WAL to arrive or failover
399+
* trigger file to appear.
400+
*/
401+
Latch recoveryWakeupLatch;
402+
395403
/*
396404
* During recovery, we keep a copy of the latest checkpoint record here.
397405
* Used by the background writer when it wants to create a restartpoint.
@@ -4840,6 +4848,7 @@ XLOGShmemInit(void)
48404848
XLogCtl->SharedRecoveryInProgress = true;
48414849
XLogCtl->Insert.currpage = (XLogPageHeader) (XLogCtl->pages);
48424850
SpinLockInit(&XLogCtl->info_lck);
4851+
InitSharedLatch(&XLogCtl->recoveryWakeupLatch);
48434852

48444853
/*
48454854
* If we are not in bootstrap mode, pg_control should already exist. Read
@@ -5814,6 +5823,13 @@ StartupXLOG(void)
58145823
(errmsg("starting archive recovery")));
58155824
}
58165825

5826+
/*
5827+
* Take ownership of the wakup latch if we're going to sleep during
5828+
* recovery.
5829+
*/
5830+
if (StandbyMode)
5831+
OwnLatch(&XLogCtl->recoveryWakeupLatch);
5832+
58175833
if (read_backup_label(&checkPointLoc))
58185834
{
58195835
/*
@@ -6274,6 +6290,13 @@ StartupXLOG(void)
62746290
if (WalRcvInProgress())
62756291
elog(PANIC, "wal receiver still active");
62766292

6293+
/*
6294+
* We don't need the latch anymore. It's not strictly necessary to disown
6295+
* it, but let's do it for the sake of tidyness.
6296+
*/
6297+
if (StandbyMode)
6298+
DisownLatch(&XLogCtl->recoveryWakeupLatch);
6299+
62776300
/*
62786301
* We are now done reading the xlog from stream. Turn off streaming
62796302
* recovery to force fetching the files (which would be required at end of
@@ -9139,6 +9162,13 @@ startupproc_quickdie(SIGNAL_ARGS)
91399162
}
91409163

91419164

9165+
/* SIGUSR1: let latch facility handle the signal */
9166+
static void
9167+
StartupProcSigUsr1Handler(SIGNAL_ARGS)
9168+
{
9169+
latch_sigusr1_handler();
9170+
}
9171+
91429172
/* SIGHUP: set flag to re-read config file at next convenient time */
91439173
static void
91449174
StartupProcSigHupHandler(SIGNAL_ARGS)
@@ -9213,7 +9243,7 @@ StartupProcessMain(void)
92139243
else
92149244
pqsignal(SIGALRM, SIG_IGN);
92159245
pqsignal(SIGPIPE, SIG_IGN);
9216-
pqsignal(SIGUSR1, SIG_IGN);
9246+
pqsignal(SIGUSR1, StartupProcSigUsr1Handler);
92179247
pqsignal(SIGUSR2, SIG_IGN);
92189248

92199249
/*
@@ -9397,16 +9427,17 @@ XLogPageRead(XLogRecPtr *RecPtr, int emode, bool fetching_ckpt,
93979427
}
93989428

93999429
/*
9400-
* Data not here yet, so check for trigger then sleep.
9430+
* Data not here yet, so check for trigger then sleep for
9431+
* five seconds like in the WAL file polling case below.
94019432
*/
94029433
if (CheckForStandbyTrigger())
94039434
goto triggered;
94049435

94059436
/*
9406-
* When streaming is active, we want to react quickly when
9407-
* the next WAL record arrives, so sleep only a bit.
9437+
* Wait for more WAL to arrive, or timeout to be reached
94089438
*/
9409-
pg_usleep(100000L); /* 100ms */
9439+
WaitLatch(&XLogCtl->recoveryWakeupLatch, 5000000L);
9440+
ResetLatch(&XLogCtl->recoveryWakeupLatch);
94109441
}
94119442
else
94129443
{
@@ -9681,3 +9712,13 @@ CheckForStandbyTrigger(void)
96819712
}
96829713
return false;
96839714
}
9715+
9716+
/*
9717+
* Wake up startup process to replay newly arrived WAL, or to notice that
9718+
* failover has been requested.
9719+
*/
9720+
void
9721+
WakeupRecovery(void)
9722+
{
9723+
SetLatch(&XLogCtl->recoveryWakeupLatch);
9724+
}

src/backend/replication/walreceiver.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
*
3131
* IDENTIFICATION
32-
* $PostgreSQL: pgsql/src/backend/replication/walreceiver.c,v 1.16 2010/07/06 19:18:57 momjian Exp $
32+
* $PostgreSQL: pgsql/src/backend/replication/walreceiver.c,v 1.17 2010/09/15 10:35:05 heikki Exp $
3333
*
3434
*-------------------------------------------------------------------------
3535
*/
@@ -529,6 +529,9 @@ XLogWalRcvFlush(void)
529529
walrcv->receivedUpto = LogstreamResult.Flush;
530530
SpinLockRelease(&walrcv->mutex);
531531

532+
/* Signal the startup process that new WAL has arrived */
533+
WakeupRecovery();
534+
532535
/* Report XLOG streaming progress in PS display */
533536
if (update_process_title)
534537
{

src/include/access/xlog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.116 2010/08/12 23:24:54 rhaas Exp $
9+
* $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.117 2010/09/15 10:35:05 heikki Exp $
1010
*/
1111
#ifndef XLOG_H
1212
#define XLOG_H
@@ -303,5 +303,6 @@ extern TimeLineID GetRecoveryTargetTLI(void);
303303

304304
extern void HandleStartupProcInterrupts(void);
305305
extern void StartupProcessMain(void);
306+
extern void WakeupRecovery(void);
306307

307308
#endif /* XLOG_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy