Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cba8fc6

Browse files
committed
Make checkpoint requests more robust.
Commit 6f6a6d8 introduced a delay of up to 2 seconds if we're trying to request a checkpoint but the checkpointer hasn't started yet (or, much less likely, our kill() call fails). However buildfarm experience shows that that's not quite enough for slow or heavily-loaded machines. There's no good reason to assume that the checkpointer won't start eventually, so we may as well make the timeout much longer, say 60 sec. However, if the caller didn't say CHECKPOINT_WAIT, it seems like a bad idea to be waiting at all, much less for as long as 60 sec. We can remove the need for that, and make this whole thing more robust, by adjusting the code so that the existence of a pending checkpoint request is clear from the contents of shared memory, and making sure that the checkpointer process will notice it at startup even if it did not get a signal. In this way there's no need for a non-CHECKPOINT_WAIT call to wait at all; if it can't send the signal, it can nonetheless assume that the checkpointer will eventually service the request. A potential downside of this change is that "kill -INT" on the checkpointer process is no longer enough to trigger a checkpoint, should anyone be relying on something so hacky. But there's no obvious reason to do it like that rather than issuing a plain old CHECKPOINT command, so we'll assume that nobody is. There doesn't seem to be a way to preserve this undocumented quasi-feature without introducing race conditions. Since a principal reason for messing with this is to prevent intermittent buildfarm failures, back-patch to all supported branches. Discussion: https://postgr.es/m/27830.1552752475@sss.pgh.pa.us
1 parent 31eb62d commit cba8fc6

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/backend/postmaster/checkpointer.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ double CheckPointCompletionTarget = 0.5;
150150
* Flags set by interrupt handlers for later service in the main loop.
151151
*/
152152
static volatile sig_atomic_t got_SIGHUP = false;
153-
static volatile sig_atomic_t checkpoint_requested = false;
154153
static volatile sig_atomic_t shutdown_requested = false;
155154

156155
/*
@@ -382,12 +381,6 @@ CheckpointerMain(void)
382381
*/
383382
UpdateSharedMemoryConfig();
384383
}
385-
if (checkpoint_requested)
386-
{
387-
checkpoint_requested = false;
388-
do_checkpoint = true;
389-
BgWriterStats.m_requested_checkpoints++;
390-
}
391384
if (shutdown_requested)
392385
{
393386
/*
@@ -401,6 +394,17 @@ CheckpointerMain(void)
401394
proc_exit(0); /* done */
402395
}
403396

397+
/*
398+
* Detect a pending checkpoint request by checking whether the flags
399+
* word in shared memory is nonzero. We shouldn't need to acquire the
400+
* ckpt_lck for this.
401+
*/
402+
if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
403+
{
404+
do_checkpoint = true;
405+
BgWriterStats.m_requested_checkpoints++;
406+
}
407+
404408
/*
405409
* Force a checkpoint if too much time has elapsed since the last one.
406410
* Note that we count a timed checkpoint in stats only when this
@@ -645,17 +649,14 @@ CheckArchiveTimeout(void)
645649
static bool
646650
ImmediateCheckpointRequested(void)
647651
{
648-
if (checkpoint_requested)
649-
{
650-
volatile CheckpointerShmemStruct *cps = CheckpointerShmem;
652+
volatile CheckpointerShmemStruct *cps = CheckpointerShmem;
651653

652-
/*
653-
* We don't need to acquire the ckpt_lck in this case because we're
654-
* only looking at a single flag bit.
655-
*/
656-
if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
657-
return true;
658-
}
654+
/*
655+
* We don't need to acquire the ckpt_lck in this case because we're only
656+
* looking at a single flag bit.
657+
*/
658+
if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
659+
return true;
659660
return false;
660661
}
661662

@@ -858,7 +859,10 @@ ReqCheckpointHandler(SIGNAL_ARGS)
858859
{
859860
int save_errno = errno;
860861

861-
checkpoint_requested = true;
862+
/*
863+
* The signalling process should have set ckpt_flags nonzero, so all we
864+
* need do is ensure that our main loop gets kicked out of any wait.
865+
*/
862866
SetLatch(MyLatch);
863867

864868
errno = save_errno;
@@ -997,31 +1001,35 @@ RequestCheckpoint(int flags)
9971001

9981002
old_failed = CheckpointerShmem->ckpt_failed;
9991003
old_started = CheckpointerShmem->ckpt_started;
1000-
CheckpointerShmem->ckpt_flags |= flags;
1004+
CheckpointerShmem->ckpt_flags |= (flags | CHECKPOINT_REQUESTED);
10011005

10021006
SpinLockRelease(&CheckpointerShmem->ckpt_lck);
10031007

10041008
/*
10051009
* Send signal to request checkpoint. It's possible that the checkpointer
10061010
* hasn't started yet, or is in process of restarting, so we will retry a
1007-
* few times if needed. Also, if not told to wait for the checkpoint to
1008-
* occur, we consider failure to send the signal to be nonfatal and merely
1009-
* LOG it.
1011+
* few times if needed. (Actually, more than a few times, since on slow
1012+
* or overloaded buildfarm machines, it's been observed that the
1013+
* checkpointer can take several seconds to start.) However, if not told
1014+
* to wait for the checkpoint to occur, we consider failure to send the
1015+
* signal to be nonfatal and merely LOG it. The checkpointer should see
1016+
* the request when it does start, with or without getting a signal.
10101017
*/
1018+
#define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
10111019
for (ntries = 0;; ntries++)
10121020
{
10131021
if (CheckpointerShmem->checkpointer_pid == 0)
10141022
{
1015-
if (ntries >= 20) /* max wait 2.0 sec */
1023+
if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
10161024
{
10171025
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1018-
"could not request checkpoint because checkpointer not running");
1026+
"could not signal for checkpoint: checkpointer is not running");
10191027
break;
10201028
}
10211029
}
10221030
else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
10231031
{
1024-
if (ntries >= 20) /* max wait 2.0 sec */
1032+
if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
10251033
{
10261034
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
10271035
"could not signal for checkpoint: %m");

src/include/access/xlog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ extern bool XLOG_DEBUG;
185185
/* These indicate the cause of a checkpoint request */
186186
#define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
187187
#define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
188+
/* We set this to ensure that ckpt_flags is not 0 if a request has been made */
189+
#define CHECKPOINT_REQUESTED 0x0100 /* Checkpoint request has been made */
188190

189191
/*
190192
* Flag bits for the record being inserted, set using XLogSetRecordFlags().

0 commit comments

Comments
 (0)