Skip to content

Commit 693e986

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 c77f218 commit 693e986

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

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

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

863867
errno = save_errno;
@@ -996,31 +1000,35 @@ RequestCheckpoint(int flags)
9961000

9971001
old_failed = CheckpointerShmem->ckpt_failed;
9981002
old_started = CheckpointerShmem->ckpt_started;
999-
CheckpointerShmem->ckpt_flags |= flags;
1003+
CheckpointerShmem->ckpt_flags |= (flags | CHECKPOINT_REQUESTED);
10001004

10011005
SpinLockRelease(&CheckpointerShmem->ckpt_lck);
10021006

10031007
/*
10041008
* Send signal to request checkpoint. It's possible that the checkpointer
10051009
* hasn't started yet, or is in process of restarting, so we will retry a
1006-
* few times if needed. Also, if not told to wait for the checkpoint to
1007-
* occur, we consider failure to send the signal to be nonfatal and merely
1008-
* LOG it.
1010+
* few times if needed. (Actually, more than a few times, since on slow
1011+
* or overloaded buildfarm machines, it's been observed that the
1012+
* checkpointer can take several seconds to start.) However, if not told
1013+
* to wait for the checkpoint to occur, we consider failure to send the
1014+
* signal to be nonfatal and merely LOG it. The checkpointer should see
1015+
* the request when it does start, with or without getting a signal.
10091016
*/
1017+
#define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
10101018
for (ntries = 0;; ntries++)
10111019
{
10121020
if (CheckpointerShmem->checkpointer_pid == 0)
10131021
{
1014-
if (ntries >= 20) /* max wait 2.0 sec */
1022+
if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
10151023
{
10161024
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1017-
"could not request checkpoint because checkpointer not running");
1025+
"could not signal for checkpoint: checkpointer is not running");
10181026
break;
10191027
}
10201028
}
10211029
else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
10221030
{
1023-
if (ntries >= 20) /* max wait 2.0 sec */
1031+
if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
10241032
{
10251033
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
10261034
"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
@@ -184,6 +184,8 @@ extern bool XLOG_DEBUG;
184184
/* These indicate the cause of a checkpoint request */
185185
#define CHECKPOINT_CAUSE_XLOG 0x0040 /* XLOG consumption */
186186
#define CHECKPOINT_CAUSE_TIME 0x0080 /* Elapsed time */
187+
/* We set this to ensure that ckpt_flags is not 0 if a request has been made */
188+
#define CHECKPOINT_REQUESTED 0x0100 /* Checkpoint request has been made */
187189

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

0 commit comments

Comments
 (0)