Skip to content

Commit 7ff23c6

Browse files
committed
Run checkpointer and bgwriter in crash recovery.
Start up the checkpointer and bgwriter during crash recovery (except in --single mode), as we do for replication. This wasn't done back in commit cdd46c7 out of caution. Now it seems like a better idea to make the environment as similar as possible in both cases. There may also be some performance advantages. Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Aleksander Alekseev <aleksander@timescale.com> Tested-by: Jakub Wartak <Jakub.Wartak@tomtom.com> Discussion: https://postgr.es/m/CA%2BhUKGJ8NRsqgkZEnsnRc2MFROBV-jCnacbYvtpptK2A9YYp9Q%40mail.gmail.com
1 parent 8b1de88 commit 7ff23c6

File tree

6 files changed

+22
-65
lines changed

6 files changed

+22
-65
lines changed

src/backend/access/transam/xlog.c

+12-21
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,6 @@ bool reachedConsistency = false;
869869

870870
static bool InRedo = false;
871871

872-
/* Have we launched bgwriter during recovery? */
873-
static bool bgwriterLaunched = false;
874-
875872
/* For WALInsertLockAcquire/Release functions */
876873
static int MyLockNo = 0;
877874
static bool holdingAllLocks = false;
@@ -7311,25 +7308,15 @@ StartupXLOG(void)
73117308
/* Also ensure XLogReceiptTime has a sane value */
73127309
XLogReceiptTime = GetCurrentTimestamp();
73137310

7311+
/* Allow ProcSendSignal() to find us, for buffer pin wakeups. */
7312+
PublishStartupProcessInformation();
7313+
73147314
/*
73157315
* Let postmaster know we've started redo now, so that it can launch
7316-
* checkpointer to perform restartpoints. We don't bother during
7317-
* crash recovery as restartpoints can only be performed during
7318-
* archive recovery. And we'd like to keep crash recovery simple, to
7319-
* avoid introducing bugs that could affect you when recovering after
7320-
* crash.
7321-
*
7322-
* After this point, we can no longer assume that we're the only
7323-
* process in addition to postmaster! Also, fsync requests are
7324-
* subsequently to be handled by the checkpointer, not locally.
7316+
* the archiver if necessary.
73257317
*/
7326-
if (ArchiveRecoveryRequested && IsUnderPostmaster)
7327-
{
7328-
PublishStartupProcessInformation();
7329-
EnableSyncRequestForwarding();
7318+
if (IsUnderPostmaster)
73307319
SendPostmasterSignal(PMSIGNAL_RECOVERY_STARTED);
7331-
bgwriterLaunched = true;
7332-
}
73337320

73347321
/*
73357322
* Allow read-only connections immediately if we're consistent
@@ -7903,7 +7890,7 @@ StartupXLOG(void)
79037890
* after we're fully out of recovery mode and already accepting
79047891
* queries.
79057892
*/
7906-
if (bgwriterLaunched)
7893+
if (ArchiveRecoveryRequested && IsUnderPostmaster)
79077894
{
79087895
if (LocalPromoteIsTriggered)
79097896
{
@@ -7927,7 +7914,11 @@ StartupXLOG(void)
79277914
CHECKPOINT_WAIT);
79287915
}
79297916
else
7930-
CreateCheckPoint(CHECKPOINT_END_OF_RECOVERY | CHECKPOINT_IMMEDIATE);
7917+
{
7918+
RequestCheckpoint(CHECKPOINT_END_OF_RECOVERY |
7919+
CHECKPOINT_IMMEDIATE |
7920+
CHECKPOINT_WAIT);
7921+
}
79317922
}
79327923

79337924
if (ArchiveRecoveryRequested)
@@ -12182,7 +12173,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1218212173
* Request a restartpoint if we've replayed too much xlog since the
1218312174
* last one.
1218412175
*/
12185-
if (bgwriterLaunched)
12176+
if (ArchiveRecoveryRequested && IsUnderPostmaster)
1218612177
{
1218712178
if (XLogCheckpointNeeded(readSegNo))
1218812179
{

src/backend/postmaster/bgwriter.c

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
*
1313
* As of Postgres 9.2 the bgwriter no longer handles checkpoints.
1414
*
15-
* The bgwriter is started by the postmaster as soon as the startup subprocess
16-
* finishes, or as soon as recovery begins if we are doing archive recovery.
17-
* It remains alive until the postmaster commands it to terminate.
1815
* Normal termination is by SIGTERM, which instructs the bgwriter to exit(0).
1916
* Emergency termination is by SIGQUIT; like any backend, the bgwriter will
2017
* simply abort and exit on SIGQUIT.

src/backend/postmaster/checkpointer.c

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
* fill WAL segments; the checkpointer itself doesn't watch for the
1111
* condition.)
1212
*
13-
* The checkpointer is started by the postmaster as soon as the startup
14-
* subprocess finishes, or as soon as recovery begins if we are doing archive
15-
* recovery. It remains alive until the postmaster commands it to terminate.
1613
* Normal termination is by SIGUSR2, which instructs the checkpointer to
1714
* execute a shutdown checkpoint and then exit(0). (All backends must be
1815
* stopped before SIGUSR2 is issued!) Emergency termination is by SIGQUIT;

src/backend/postmaster/postmaster.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,12 @@ PostmasterMain(int argc, char *argv[])
14031403
*/
14041404
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STARTING);
14051405

1406+
/* Start bgwriter and checkpointer so they can help with recovery */
1407+
if (CheckpointerPID == 0)
1408+
CheckpointerPID = StartCheckpointer();
1409+
if (BgWriterPID == 0)
1410+
BgWriterPID = StartBackgroundWriter();
1411+
14061412
/*
14071413
* We're ready to rock and roll...
14081414
*/
@@ -1765,7 +1771,7 @@ ServerLoop(void)
17651771
* fails, we'll just try again later. Likewise for the checkpointer.
17661772
*/
17671773
if (pmState == PM_RUN || pmState == PM_RECOVERY ||
1768-
pmState == PM_HOT_STANDBY)
1774+
pmState == PM_HOT_STANDBY || pmState == PM_STARTUP)
17691775
{
17701776
if (CheckpointerPID == 0)
17711777
CheckpointerPID = StartCheckpointer();
@@ -5161,15 +5167,6 @@ sigusr1_handler(SIGNAL_ARGS)
51615167
FatalError = false;
51625168
AbortStartTime = 0;
51635169

5164-
/*
5165-
* Crank up the background tasks. It doesn't matter if this fails,
5166-
* we'll just try again later.
5167-
*/
5168-
Assert(CheckpointerPID == 0);
5169-
CheckpointerPID = StartCheckpointer();
5170-
Assert(BgWriterPID == 0);
5171-
BgWriterPID = StartBackgroundWriter();
5172-
51735170
/*
51745171
* Start the archiver if we're responsible for (re-)archiving received
51755172
* files.

src/backend/storage/sync/sync.c

+3-27
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ InitSync(void)
129129
{
130130
/*
131131
* Create pending-operations hashtable if we need it. Currently, we need
132-
* it if we are standalone (not under a postmaster) or if we are a startup
133-
* or checkpointer auxiliary process.
132+
* it if we are standalone (not under a postmaster) or if we are a
133+
* checkpointer auxiliary process.
134134
*/
135-
if (!IsUnderPostmaster || AmStartupProcess() || AmCheckpointerProcess())
135+
if (!IsUnderPostmaster || AmCheckpointerProcess())
136136
{
137137
HASHCTL hash_ctl;
138138

@@ -589,27 +589,3 @@ RegisterSyncRequest(const FileTag *ftag, SyncRequestType type,
589589

590590
return ret;
591591
}
592-
593-
/*
594-
* In archive recovery, we rely on checkpointer to do fsyncs, but we will have
595-
* already created the pendingOps during initialization of the startup
596-
* process. Calling this function drops the local pendingOps so that
597-
* subsequent requests will be forwarded to checkpointer.
598-
*/
599-
void
600-
EnableSyncRequestForwarding(void)
601-
{
602-
/* Perform any pending fsyncs we may have queued up, then drop table */
603-
if (pendingOps)
604-
{
605-
ProcessSyncRequests();
606-
hash_destroy(pendingOps);
607-
}
608-
pendingOps = NULL;
609-
610-
/*
611-
* We should not have any pending unlink requests, since mdunlink doesn't
612-
* queue unlink requests when isRedo.
613-
*/
614-
Assert(pendingUnlinks == NIL);
615-
}

src/include/storage/sync.h

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ extern void SyncPreCheckpoint(void);
6060
extern void SyncPostCheckpoint(void);
6161
extern void ProcessSyncRequests(void);
6262
extern void RememberSyncRequest(const FileTag *ftag, SyncRequestType type);
63-
extern void EnableSyncRequestForwarding(void);
6463
extern bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type,
6564
bool retryOnError);
6665

0 commit comments

Comments
 (0)