PostgreSQL Source Code git master
checkpointer.c File Reference
#include "postgres.h"
#include <sys/time.h>
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/auxprocess.h"
#include "postmaster/bgwriter.h"
#include "postmaster/interrupt.h"
#include "replication/syncrep.h"
#include "storage/bufmgr.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "storage/smgr.h"
#include "storage/spin.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
Include dependency graph for checkpointer.c:

Go to the source code of this file.

Data Structures

struct  CheckpointerRequest
 
struct  CheckpointerShmemStruct
 

Macros

#define WRITES_PER_ABSORB   1000
 
#define MAX_SIGNAL_TRIES   600 /* max wait 60.0 sec */
 

Functions

static void HandleCheckpointerInterrupts (void)
 
static void CheckArchiveTimeout (void)
 
static bool IsCheckpointOnSchedule (double progress)
 
static bool ImmediateCheckpointRequested (void)
 
static bool CompactCheckpointerRequestQueue (void)
 
static void UpdateSharedMemoryConfig (void)
 
static void ReqShutdownXLOG (SIGNAL_ARGS)
 
void CheckpointerMain (char *startup_data, size_t startup_data_len)
 
void CheckpointWriteDelay (int flags, double progress)
 
Size CheckpointerShmemSize (void)
 
void CheckpointerShmemInit (void)
 
void RequestCheckpoint (int flags)
 
bool ForwardSyncRequest (const FileTag *ftag, SyncRequestType type)
 
void AbsorbSyncRequests (void)
 
bool FirstCallSinceLastCheckpoint (void)
 

Variables

static CheckpointerShmemStructCheckpointerShmem
 
int CheckPointTimeout = 300
 
int CheckPointWarning = 30
 
double CheckPointCompletionTarget = 0.9
 
static bool ckpt_active = false
 
static volatile sig_atomic_t ShutdownXLOGPending = false
 
static pg_time_t ckpt_start_time
 
static XLogRecPtr ckpt_start_recptr
 
static double ckpt_cached_elapsed
 
static pg_time_t last_checkpoint_time
 
static pg_time_t last_xlog_switch_time
 

Macro Definition Documentation

◆ MAX_SIGNAL_TRIES

#define MAX_SIGNAL_TRIES   600 /* max wait 60.0 sec */

◆ WRITES_PER_ABSORB

#define WRITES_PER_ABSORB   1000

Definition at line 135 of file checkpointer.c.

Function Documentation

◆ AbsorbSyncRequests()

void AbsorbSyncRequests ( void  )

Definition at line 1322 of file checkpointer.c.

1323{
1324 CheckpointerRequest *requests = NULL;
1325 CheckpointerRequest *request;
1326 int n;
1327
1328 if (!AmCheckpointerProcess())
1329 return;
1330
1331 LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1332
1333 /*
1334 * We try to avoid holding the lock for a long time by copying the request
1335 * array, and processing the requests after releasing the lock.
1336 *
1337 * Once we have cleared the requests from shared memory, we have to PANIC
1338 * if we then fail to absorb them (eg, because our hashtable runs out of
1339 * memory). This is because the system cannot run safely if we are unable
1340 * to fsync what we have been told to fsync. Fortunately, the hashtable
1341 * is so small that the problem is quite unlikely to arise in practice.
1342 */
1344 if (n > 0)
1345 {
1346 requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1347 memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1348 }
1349
1351
1353
1354 LWLockRelease(CheckpointerCommLock);
1355
1356 for (request = requests; n > 0; request++, n--)
1357 RememberSyncRequest(&request->ftag, request->type);
1358
1360
1361 if (requests)
1362 pfree(requests);
1363}
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:132
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
#define AmCheckpointerProcess()
Definition: miscadmin.h:387
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
SyncRequestType type
Definition: checkpointer.c:108
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:129
void RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
Definition: sync.c:487

References AmCheckpointerProcess, CheckpointerShmem, END_CRIT_SECTION, CheckpointerRequest::ftag, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), CheckpointerShmemStruct::num_requests, palloc(), pfree(), RememberSyncRequest(), CheckpointerShmemStruct::requests, START_CRIT_SECTION, and CheckpointerRequest::type.

Referenced by CheckpointerMain(), CheckpointWriteDelay(), CreateCheckPoint(), ProcessSyncRequests(), SyncPostCheckpoint(), and SyncPreCheckpoint().

◆ CheckArchiveTimeout()

static void CheckArchiveTimeout ( void  )
static

Definition at line 679 of file checkpointer.c.

680{
682 pg_time_t last_time;
683 XLogRecPtr last_switch_lsn;
684
686 return;
687
688 now = (pg_time_t) time(NULL);
689
690 /* First we do a quick check using possibly-stale local state. */
692 return;
693
694 /*
695 * Update local state ... note that last_xlog_switch_time is the last time
696 * a switch was performed *or requested*.
697 */
698 last_time = GetLastSegSwitchData(&last_switch_lsn);
699
701
702 /* Now we can do the real checks */
704 {
705 /*
706 * Switch segment only when "important" WAL has been logged since the
707 * last segment switch (last_switch_lsn points to end of segment
708 * switch occurred in).
709 */
710 if (GetLastImportantRecPtr() > last_switch_lsn)
711 {
712 XLogRecPtr switchpoint;
713
714 /* mark switch as unimportant, avoids triggering checkpoints */
715 switchpoint = RequestXLogSwitch(true);
716
717 /*
718 * If the returned pointer points exactly to a segment boundary,
719 * assume nothing happened.
720 */
721 if (XLogSegmentOffset(switchpoint, wal_segment_size) != 0)
722 elog(DEBUG1, "write-ahead log switch forced (\"archive_timeout\"=%d)",
724 }
725
726 /*
727 * Update state in any case, so we don't retry constantly when the
728 * system is idle.
729 */
731 }
732}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1608
#define Max(x, y)
Definition: c.h:955
static pg_time_t last_xlog_switch_time
Definition: checkpointer.c:156
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:225
int64 pg_time_t
Definition: pgtime.h:23
bool RecoveryInProgress(void)
Definition: xlog.c:6355
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:8081
int wal_segment_size
Definition: xlog.c:143
int XLogArchiveTimeout
Definition: xlog.c:118
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
Definition: xlog.c:6606
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:6577
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References DEBUG1, elog, GetLastImportantRecPtr(), GetLastSegSwitchData(), last_xlog_switch_time, Max, now(), RecoveryInProgress(), RequestXLogSwitch(), wal_segment_size, XLogArchiveTimeout, and XLogSegmentOffset.

Referenced by CheckpointerMain(), and CheckpointWriteDelay().

◆ CheckpointerMain()

void CheckpointerMain ( char *  startup_data,
size_t  startup_data_len 
)

Definition at line 178 of file checkpointer.c.

179{
180 sigjmp_buf local_sigjmp_buf;
181 MemoryContext checkpointer_context;
182
183 Assert(startup_data_len == 0);
184
187
189
190 /*
191 * Properly accept or ignore signals the postmaster might send us
192 *
193 * Note: we deliberately ignore SIGTERM, because during a standard Unix
194 * system shutdown cycle, init will SIGTERM all processes at once. We
195 * want to wait for the backends to exit, whereupon the postmaster will
196 * tell us it's okay to shut down (via SIGUSR2).
197 */
199 pqsignal(SIGINT, ReqShutdownXLOG);
200 pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
201 /* SIGQUIT handler was already set up by InitPostmasterChild */
202 pqsignal(SIGALRM, SIG_IGN);
203 pqsignal(SIGPIPE, SIG_IGN);
206
207 /*
208 * Reset some signals that are accepted by postmaster but not here
209 */
210 pqsignal(SIGCHLD, SIG_DFL);
211
212 /*
213 * Initialize so that first time-driven event happens at the correct time.
214 */
216
217 /*
218 * Write out stats after shutdown. This needs to be called by exactly one
219 * process during a normal shutdown, and since checkpointer is shut down
220 * very late...
221 *
222 * While e.g. walsenders are active after the shutdown checkpoint has been
223 * written (and thus could produce more stats), checkpointer stays around
224 * after the shutdown checkpoint has been written. postmaster will only
225 * signal checkpointer to exit after all processes that could emit stats
226 * have been shut down.
227 */
229
230 /*
231 * Create a memory context that we will do all our work in. We do this so
232 * that we can reset the context during error recovery and thereby avoid
233 * possible memory leaks. Formerly this code just ran in
234 * TopMemoryContext, but resetting that would be a really bad idea.
235 */
236 checkpointer_context = AllocSetContextCreate(TopMemoryContext,
237 "Checkpointer",
239 MemoryContextSwitchTo(checkpointer_context);
240
241 /*
242 * If an exception is encountered, processing resumes here.
243 *
244 * You might wonder why this isn't coded as an infinite loop around a
245 * PG_TRY construct. The reason is that this is the bottom of the
246 * exception stack, and so with PG_TRY there would be no exception handler
247 * in force at all during the CATCH part. By leaving the outermost setjmp
248 * always active, we have at least some chance of recovering from an error
249 * during error recovery. (If we get into an infinite loop thereby, it
250 * will soon be stopped by overflow of elog.c's internal state stack.)
251 *
252 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
253 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
254 * signals other than SIGQUIT will be blocked until we complete error
255 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
256 * call redundant, but it is not since InterruptPending might be set
257 * already.
258 */
259 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
260 {
261 /* Since not using PG_TRY, must reset error stack by hand */
262 error_context_stack = NULL;
263
264 /* Prevent interrupts while cleaning up */
266
267 /* Report the error to the server log */
269
270 /*
271 * These operations are really just a minimal subset of
272 * AbortTransaction(). We don't have very many resources to worry
273 * about in checkpointer, but we do have LWLocks, buffers, and temp
274 * files.
275 */
281 AtEOXact_Buffers(false);
283 AtEOXact_Files(false);
284 AtEOXact_HashTables(false);
285
286 /* Warn any waiting backends that the checkpoint failed. */
287 if (ckpt_active)
288 {
293
295
296 ckpt_active = false;
297 }
298
299 /*
300 * Now return to normal top-level context and clear ErrorContext for
301 * next time.
302 */
303 MemoryContextSwitchTo(checkpointer_context);
305
306 /* Flush any leaked data in the top-level context */
307 MemoryContextReset(checkpointer_context);
308
309 /* Now we can allow interrupts again */
311
312 /*
313 * Sleep at least 1 second after any error. A write error is likely
314 * to be repeated, and we don't want to be filling the error logs as
315 * fast as we can.
316 */
317 pg_usleep(1000000L);
318 }
319
320 /* We can now handle ereport(ERROR) */
321 PG_exception_stack = &local_sigjmp_buf;
322
323 /*
324 * Unblock signals (they were blocked when the postmaster forked us)
325 */
326 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
327
328 /*
329 * Ensure all shared memory values are set correctly for the config. Doing
330 * this here ensures no race conditions from other concurrent updaters.
331 */
333
334 /*
335 * Advertise our proc number that backends can use to wake us up while
336 * we're sleeping.
337 */
339
340 /*
341 * Loop until we've been asked to write the shutdown checkpoint or
342 * terminate.
343 */
344 for (;;)
345 {
346 bool do_checkpoint = false;
347 int flags = 0;
349 int elapsed_secs;
350 int cur_timeout;
351 bool chkpt_or_rstpt_requested = false;
352 bool chkpt_or_rstpt_timed = false;
353
354 /* Clear any already-pending wakeups */
356
357 /*
358 * Process any requests or signals received recently.
359 */
361
364 break;
365
366 /*
367 * Detect a pending checkpoint request by checking whether the flags
368 * word in shared memory is nonzero. We shouldn't need to acquire the
369 * ckpt_lck for this.
370 */
371 if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
372 {
373 do_checkpoint = true;
374 chkpt_or_rstpt_requested = true;
375 }
376
377 /*
378 * Force a checkpoint if too much time has elapsed since the last one.
379 * Note that we count a timed checkpoint in stats only when this
380 * occurs without an external request, but we set the CAUSE_TIME flag
381 * bit even if there is also an external request.
382 */
383 now = (pg_time_t) time(NULL);
384 elapsed_secs = now - last_checkpoint_time;
385 if (elapsed_secs >= CheckPointTimeout)
386 {
387 if (!do_checkpoint)
388 chkpt_or_rstpt_timed = true;
389 do_checkpoint = true;
390 flags |= CHECKPOINT_CAUSE_TIME;
391 }
392
393 /*
394 * Do a checkpoint if requested.
395 */
396 if (do_checkpoint)
397 {
398 bool ckpt_performed = false;
399 bool do_restartpoint;
400
401 /* Check if we should perform a checkpoint or a restartpoint. */
402 do_restartpoint = RecoveryInProgress();
403
404 /*
405 * Atomically fetch the request flags to figure out what kind of a
406 * checkpoint we should perform, and increase the started-counter
407 * to acknowledge that we've started a new checkpoint.
408 */
414
416
417 /*
418 * The end-of-recovery checkpoint is a real checkpoint that's
419 * performed while we're still in recovery.
420 */
421 if (flags & CHECKPOINT_END_OF_RECOVERY)
422 do_restartpoint = false;
423
424 if (chkpt_or_rstpt_timed)
425 {
426 chkpt_or_rstpt_timed = false;
427 if (do_restartpoint)
429 else
431 }
432
433 if (chkpt_or_rstpt_requested)
434 {
435 chkpt_or_rstpt_requested = false;
436 if (do_restartpoint)
438 else
440 }
441
442 /*
443 * We will warn if (a) too soon since last checkpoint (whatever
444 * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
445 * since the last checkpoint start. Note in particular that this
446 * implementation will not generate warnings caused by
447 * CheckPointTimeout < CheckPointWarning.
448 */
449 if (!do_restartpoint &&
450 (flags & CHECKPOINT_CAUSE_XLOG) &&
451 elapsed_secs < CheckPointWarning)
452 ereport(LOG,
453 (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
454 "checkpoints are occurring too frequently (%d seconds apart)",
455 elapsed_secs,
456 elapsed_secs),
457 errhint("Consider increasing the configuration parameter \"%s\".", "max_wal_size")));
458
459 /*
460 * Initialize checkpointer-private variables used during
461 * checkpoint.
462 */
463 ckpt_active = true;
464 if (do_restartpoint)
466 else
470
471 /*
472 * Do the checkpoint.
473 */
474 if (!do_restartpoint)
475 ckpt_performed = CreateCheckPoint(flags);
476 else
477 ckpt_performed = CreateRestartPoint(flags);
478
479 /*
480 * After any checkpoint, free all smgr objects. Otherwise we
481 * would never do so for dropped relations, as the checkpointer
482 * does not process shared invalidation messages or call
483 * AtEOXact_SMgr().
484 */
486
487 /*
488 * Indicate checkpoint completion to any waiting backends.
489 */
493
495
496 if (!do_restartpoint)
497 {
498 /*
499 * Note we record the checkpoint start time not end time as
500 * last_checkpoint_time. This is so that time-driven
501 * checkpoints happen at a predictable spacing.
502 */
504
505 if (ckpt_performed)
507 }
508 else
509 {
510 if (ckpt_performed)
511 {
512 /*
513 * The same as for checkpoint. Please see the
514 * corresponding comment.
515 */
517
519 }
520 else
521 {
522 /*
523 * We were not able to perform the restartpoint
524 * (checkpoints throw an ERROR in case of error). Most
525 * likely because we have not received any new checkpoint
526 * WAL records since the last restartpoint. Try again in
527 * 15 s.
528 */
530 }
531 }
532
533 ckpt_active = false;
534
535 /*
536 * We may have received an interrupt during the checkpoint and the
537 * latch might have been reset (e.g. in CheckpointWriteDelay).
538 */
541 break;
542 }
543
544 /* Check for archive_timeout and switch xlog files if necessary. */
546
547 /* Report pending statistics to the cumulative stats system */
549 pgstat_report_wal(true);
550
551 /*
552 * If any checkpoint flags have been set, redo the loop to handle the
553 * checkpoint without sleeping.
554 */
555 if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
556 continue;
557
558 /*
559 * Sleep until we are signaled or it's time for another checkpoint or
560 * xlog file switch.
561 */
562 now = (pg_time_t) time(NULL);
563 elapsed_secs = now - last_checkpoint_time;
564 if (elapsed_secs >= CheckPointTimeout)
565 continue; /* no sleep for us ... */
566 cur_timeout = CheckPointTimeout - elapsed_secs;
568 {
569 elapsed_secs = now - last_xlog_switch_time;
570 if (elapsed_secs >= XLogArchiveTimeout)
571 continue; /* no sleep for us ... */
572 cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
573 }
574
575 (void) WaitLatch(MyLatch,
577 cur_timeout * 1000L /* convert to ms */ ,
578 WAIT_EVENT_CHECKPOINTER_MAIN);
579 }
580
581 /*
582 * From here on, elog(ERROR) should end with exit(1), not send control
583 * back to the sigsetjmp block above.
584 */
585 ExitOnAnyError = true;
586
588 {
589 /*
590 * Close down the database.
591 *
592 * Since ShutdownXLOG() creates restartpoint or checkpoint, and
593 * updates the statistics, increment the checkpoint request and flush
594 * out pending statistic.
595 */
597 ShutdownXLOG(0, 0);
599 pgstat_report_wal(true);
600
601 /*
602 * Tell postmaster that we're done.
603 */
605 ShutdownXLOGPending = false;
606 }
607
608 /*
609 * Wait until we're asked to shut down. By separating the writing of the
610 * shutdown checkpoint from checkpointer exiting, checkpointer can perform
611 * some should-be-as-late-as-possible work like writing out stats.
612 */
613 for (;;)
614 {
615 /* Clear any already-pending wakeups */
617
619
621 break;
622
623 (void) WaitLatch(MyLatch,
625 0,
626 WAIT_EVENT_CHECKPOINTER_SHUTDOWN);
627 }
628
629 /* Normal exit from the checkpointer is here */
630 proc_exit(0); /* done */
631}
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3559
void UnlockBuffers(void)
Definition: bufmgr.c:5072
#define Min(x, y)
Definition: c.h:961
#define Assert(condition)
Definition: c.h:815
static void UpdateSharedMemoryConfig(void)
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:152
static void ReqShutdownXLOG(SIGNAL_ARGS)
Definition: checkpointer.c:916
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:679
static double ckpt_cached_elapsed
Definition: checkpointer.c:153
static bool ckpt_active
Definition: checkpointer.c:147
static void HandleCheckpointerInterrupts(void)
Definition: checkpointer.c:637
static volatile sig_atomic_t ShutdownXLOGPending
Definition: checkpointer.c:148
void AbsorbSyncRequests(void)
int CheckPointWarning
Definition: checkpointer.c:141
int CheckPointTimeout
Definition: checkpointer.c:140
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:155
static pg_time_t ckpt_start_time
Definition: checkpointer.c:151
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1912
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
void EmitErrorReport(void)
Definition: elog.c:1687
ErrorContextCallback * error_context_stack
Definition: elog.c:94
void FlushErrorState(void)
Definition: elog.c:1867
int errhint(const char *fmt,...)
Definition: elog.c:1317
sigjmp_buf * PG_exception_stack
Definition: elog.c:96
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3187
int MyProcPid
Definition: globals.c:46
ProcNumber MyProcNumber
Definition: globals.c:89
bool ExitOnAnyError
Definition: globals.c:122
struct Latch * MyLatch
Definition: globals.c:62
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
void proc_exit(int code)
Definition: ipc.c:104
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
void LWLockReleaseAll(void)
Definition: lwlock.c:1876
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
MemoryContext TopMemoryContext
Definition: mcxt.c:149
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
@ B_CHECKPOINTER
Definition: miscadmin.h:362
BackendType MyBackendType
Definition: miscinit.c:64
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void pgstat_before_server_shutdown(int code, Datum arg)
Definition: pgstat.c:560
void pgstat_report_checkpointer(void)
PgStat_CheckpointerStats PendingCheckpointerStats
void pgstat_report_wal(bool force)
Definition: pgstat_wal.c:48
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:165
@ PMSIGNAL_XLOG_IS_SHUTDOWN
Definition: pmsignal.h:43
#define pqsignal
Definition: port.h:521
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1002
void pg_usleep(long microsec)
Definition: signal.c:53
void smgrdestroyall(void)
Definition: smgr.c:335
void AtEOXact_SMgr(void)
Definition: smgr.c:835
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
PROC_HDR * ProcGlobal
Definition: proc.c:78
ConditionVariable done_cv
Definition: checkpointer.c:125
ConditionVariable start_cv
Definition: checkpointer.c:124
ProcNumber checkpointerProc
Definition: proc.h:408
PgStat_Counter restartpoints_requested
Definition: pgstat.h:260
PgStat_Counter num_requested
Definition: pgstat.h:257
PgStat_Counter num_performed
Definition: pgstat.h:258
PgStat_Counter restartpoints_timed
Definition: pgstat.h:259
PgStat_Counter num_timed
Definition: pgstat.h:256
PgStat_Counter restartpoints_performed
Definition: pgstat.h:261
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
#define SIGCHLD
Definition: win32_port.h:168
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGUSR1
Definition: win32_port.h:170
#define SIGALRM
Definition: win32_port.h:164
#define SIGUSR2
Definition: win32_port.h:171
bool CreateRestartPoint(int flags)
Definition: xlog.c:7612
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:6503
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:6623
bool CreateCheckPoint(int flags)
Definition: xlog.c:6910
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:148
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:140
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:149
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References AbsorbSyncRequests(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), AuxiliaryProcessMainCommon(), B_CHECKPOINTER, before_shmem_exit(), CheckArchiveTimeout(), CHECKPOINT_CAUSE_TIME, CHECKPOINT_CAUSE_XLOG, CHECKPOINT_END_OF_RECOVERY, CheckpointerShmemStruct::checkpointer_pid, PROC_HDR::checkpointerProc, CheckpointerShmem, CheckPointTimeout, CheckPointWarning, ckpt_active, ckpt_cached_elapsed, CheckpointerShmemStruct::ckpt_done, CheckpointerShmemStruct::ckpt_failed, CheckpointerShmemStruct::ckpt_flags, CheckpointerShmemStruct::ckpt_lck, ckpt_start_recptr, ckpt_start_time, CheckpointerShmemStruct::ckpt_started, ConditionVariableBroadcast(), ConditionVariableCancelSleep(), CreateCheckPoint(), CreateRestartPoint(), CheckpointerShmemStruct::done_cv, EmitErrorReport(), ereport, errhint(), errmsg_plural(), error_context_stack, ExitOnAnyError, FlushErrorState(), GetInsertRecPtr(), GetXLogReplayRecPtr(), HandleCheckpointerInterrupts(), HOLD_INTERRUPTS, last_checkpoint_time, last_xlog_switch_time, LOG, LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), Min, MyBackendType, MyLatch, MyProcNumber, MyProcPid, now(), PgStat_CheckpointerStats::num_performed, PgStat_CheckpointerStats::num_requested, PgStat_CheckpointerStats::num_timed, PendingCheckpointerStats, PG_exception_stack, pg_usleep(), pgstat_before_server_shutdown(), pgstat_report_checkpointer(), pgstat_report_wait_end(), pgstat_report_wal(), PMSIGNAL_XLOG_IS_SHUTDOWN, pqsignal, proc_exit(), ProcGlobal, procsignal_sigusr1_handler(), RecoveryInProgress(), ReleaseAuxProcessResources(), ReqShutdownXLOG(), ResetLatch(), PgStat_CheckpointerStats::restartpoints_performed, PgStat_CheckpointerStats::restartpoints_requested, PgStat_CheckpointerStats::restartpoints_timed, RESUME_INTERRUPTS, SendPostmasterSignal(), ShutdownRequestPending, ShutdownXLOG(), ShutdownXLOGPending, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, smgrdestroyall(), SpinLockAcquire, SpinLockRelease, CheckpointerShmemStruct::start_cv, TopMemoryContext, UnBlockSig, UnlockBuffers(), UpdateSharedMemoryConfig(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and XLogArchiveTimeout.

◆ CheckpointerShmemInit()

void CheckpointerShmemInit ( void  )

Definition at line 952 of file checkpointer.c.

953{
955 bool found;
956
958 ShmemInitStruct("Checkpointer Data",
959 size,
960 &found);
961
962 if (!found)
963 {
964 /*
965 * First time through, so initialize. Note that we zero the whole
966 * requests array; this is so that CompactCheckpointerRequestQueue can
967 * assume that any pad bytes in the request structs are zeroes.
968 */
974 }
975}
#define MemSet(start, val, len)
Definition: c.h:977
size_t Size
Definition: c.h:562
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:933
void ConditionVariableInit(ConditionVariable *cv)
int NBuffers
Definition: globals.c:141
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
static pg_noinline void Size size
Definition: slab.c:607
#define SpinLockInit(lock)
Definition: spin.h:57

References CheckpointerShmem, CheckpointerShmemSize(), CheckpointerShmemStruct::ckpt_lck, ConditionVariableInit(), CheckpointerShmemStruct::done_cv, CheckpointerShmemStruct::max_requests, MemSet, NBuffers, ShmemInitStruct(), size, SpinLockInit, and CheckpointerShmemStruct::start_cv.

Referenced by CreateOrAttachShmemStructs().

◆ CheckpointerShmemSize()

Size CheckpointerShmemSize ( void  )

Definition at line 933 of file checkpointer.c.

934{
935 Size size;
936
937 /*
938 * Currently, the size of the requests[] array is arbitrarily set equal to
939 * NBuffers. This may prove too large or small ...
940 */
941 size = offsetof(CheckpointerShmemStruct, requests);
943
944 return size;
945}
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
Size mul_size(Size s1, Size s2)
Definition: shmem.c:505

References add_size(), mul_size(), NBuffers, and size.

Referenced by CalculateShmemSize(), and CheckpointerShmemInit().

◆ CheckpointWriteDelay()

void CheckpointWriteDelay ( int  flags,
double  progress 
)

Definition at line 767 of file checkpointer.c.

768{
769 static int absorb_counter = WRITES_PER_ABSORB;
770
771 /* Do nothing if checkpoint is being executed by non-checkpointer process */
773 return;
774
775 /*
776 * Perform the usual duties and take a nap, unless we're behind schedule,
777 * in which case we just try to catch up as quickly as possible.
778 */
779 if (!(flags & CHECKPOINT_IMMEDIATE) &&
784 {
786 {
787 ConfigReloadPending = false;
789 /* update shmem copies of config variables */
791 }
792
794 absorb_counter = WRITES_PER_ABSORB;
795
797
798 /* Report interim statistics to the cumulative stats system */
800
801 /*
802 * This sleep used to be connected to bgwriter_delay, typically 200ms.
803 * That resulted in more frequent wakeups if not much work to do.
804 * Checkpointer and bgwriter are no longer related so take the Big
805 * Sleep.
806 */
808 100,
809 WAIT_EVENT_CHECKPOINT_WRITE_DELAY);
811 }
812 else if (--absorb_counter <= 0)
813 {
814 /*
815 * Absorb pending fsync requests after each WRITES_PER_ABSORB write
816 * operations even when we don't sleep, to prevent overflow of the
817 * fsync request queue.
818 */
820 absorb_counter = WRITES_PER_ABSORB;
821 }
822
823 /* Check for barrier events. */
826}
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:740
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:837
#define WRITES_PER_ABSORB
Definition: checkpointer.c:135
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:39
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
static int progress
Definition: pgbench.c:262
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:496
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:141

References AbsorbSyncRequests(), AmCheckpointerProcess, CheckArchiveTimeout(), CHECKPOINT_IMMEDIATE, ConfigReloadPending, ImmediateCheckpointRequested(), IsCheckpointOnSchedule(), MyLatch, PGC_SIGHUP, pgstat_report_checkpointer(), ProcessConfigFile(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, progress, ResetLatch(), ShutdownRequestPending, ShutdownXLOGPending, UpdateSharedMemoryConfig(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and WRITES_PER_ABSORB.

Referenced by BufferSync().

◆ CompactCheckpointerRequestQueue()

static bool CompactCheckpointerRequestQueue ( void  )
static

Definition at line 1212 of file checkpointer.c.

1213{
1214 struct CheckpointerSlotMapping
1215 {
1216 CheckpointerRequest request;
1217 int slot;
1218 };
1219
1220 int n,
1221 preserve_count;
1222 int num_skipped = 0;
1223 HASHCTL ctl;
1224 HTAB *htab;
1225 bool *skip_slot;
1226
1227 /* must hold CheckpointerCommLock in exclusive mode */
1228 Assert(LWLockHeldByMe(CheckpointerCommLock));
1229
1230 /* Avoid memory allocations in a critical section. */
1231 if (CritSectionCount > 0)
1232 return false;
1233
1234 /* Initialize skip_slot array */
1235 skip_slot = palloc0(sizeof(bool) * CheckpointerShmem->num_requests);
1236
1237 /* Initialize temporary hash table */
1238 ctl.keysize = sizeof(CheckpointerRequest);
1239 ctl.entrysize = sizeof(struct CheckpointerSlotMapping);
1241
1242 htab = hash_create("CompactCheckpointerRequestQueue",
1244 &ctl,
1246
1247 /*
1248 * The basic idea here is that a request can be skipped if it's followed
1249 * by a later, identical request. It might seem more sensible to work
1250 * backwards from the end of the queue and check whether a request is
1251 * *preceded* by an earlier, identical request, in the hopes of doing less
1252 * copying. But that might change the semantics, if there's an
1253 * intervening SYNC_FORGET_REQUEST or SYNC_FILTER_REQUEST, so we do it
1254 * this way. It would be possible to be even smarter if we made the code
1255 * below understand the specific semantics of such requests (it could blow
1256 * away preceding entries that would end up being canceled anyhow), but
1257 * it's not clear that the extra complexity would buy us anything.
1258 */
1259 for (n = 0; n < CheckpointerShmem->num_requests; n++)
1260 {
1261 CheckpointerRequest *request;
1262 struct CheckpointerSlotMapping *slotmap;
1263 bool found;
1264
1265 /*
1266 * We use the request struct directly as a hashtable key. This
1267 * assumes that any padding bytes in the structs are consistently the
1268 * same, which should be okay because we zeroed them in
1269 * CheckpointerShmemInit. Note also that RelFileLocator had better
1270 * contain no pad bytes.
1271 */
1272 request = &CheckpointerShmem->requests[n];
1273 slotmap = hash_search(htab, request, HASH_ENTER, &found);
1274 if (found)
1275 {
1276 /* Duplicate, so mark the previous occurrence as skippable */
1277 skip_slot[slotmap->slot] = true;
1278 num_skipped++;
1279 }
1280 /* Remember slot containing latest occurrence of this request value */
1281 slotmap->slot = n;
1282 }
1283
1284 /* Done with the hash table. */
1285 hash_destroy(htab);
1286
1287 /* If no duplicates, we're out of luck. */
1288 if (!num_skipped)
1289 {
1290 pfree(skip_slot);
1291 return false;
1292 }
1293
1294 /* We found some duplicates; remove them. */
1295 preserve_count = 0;
1296 for (n = 0; n < CheckpointerShmem->num_requests; n++)
1297 {
1298 if (skip_slot[n])
1299 continue;
1300 CheckpointerShmem->requests[preserve_count++] = CheckpointerShmem->requests[n];
1301 }
1303 (errmsg_internal("compacted fsync request queue from %d entries to %d entries",
1304 CheckpointerShmem->num_requests, preserve_count)));
1305 CheckpointerShmem->num_requests = preserve_count;
1306
1307 /* Cleanup. */
1308 pfree(skip_slot);
1309 return true;
1310}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:865
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
volatile uint32 CritSectionCount
Definition: globals.c:44
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1893
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
tree ctl
Definition: radixtree.h:1838
Definition: dynahash.c:220

References Assert, CheckpointerShmem, CritSectionCount, ctl, CurrentMemoryContext, DEBUG1, ereport, errmsg_internal(), HASH_BLOBS, HASH_CONTEXT, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), LWLockHeldByMe(), CheckpointerShmemStruct::num_requests, palloc0(), pfree(), and CheckpointerShmemStruct::requests.

Referenced by ForwardSyncRequest().

◆ FirstCallSinceLastCheckpoint()

bool FirstCallSinceLastCheckpoint ( void  )

Definition at line 1388 of file checkpointer.c.

1389{
1390 static int ckpt_done = 0;
1391 int new_done;
1392 bool FirstCall = false;
1393
1395 new_done = CheckpointerShmem->ckpt_done;
1397
1398 if (new_done != ckpt_done)
1399 FirstCall = true;
1400
1401 ckpt_done = new_done;
1402
1403 return FirstCall;
1404}

References CheckpointerShmem, CheckpointerShmemStruct::ckpt_done, CheckpointerShmemStruct::ckpt_lck, SpinLockAcquire, and SpinLockRelease.

Referenced by BackgroundWriterMain().

◆ ForwardSyncRequest()

bool ForwardSyncRequest ( const FileTag ftag,
SyncRequestType  type 
)

Definition at line 1145 of file checkpointer.c.

1146{
1147 CheckpointerRequest *request;
1148 bool too_full;
1149
1150 if (!IsUnderPostmaster)
1151 return false; /* probably shouldn't even get here */
1152
1154 elog(ERROR, "ForwardSyncRequest must not be called in checkpointer");
1155
1156 LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1157
1158 /*
1159 * If the checkpointer isn't running or the request queue is full, the
1160 * backend will have to perform its own fsync request. But before forcing
1161 * that to happen, we can try to compact the request queue.
1162 */
1166 {
1167 LWLockRelease(CheckpointerCommLock);
1168 return false;
1169 }
1170
1171 /* OK, insert request */
1173 request->ftag = *ftag;
1174 request->type = type;
1175
1176 /* If queue is more than half full, nudge the checkpointer to empty it */
1177 too_full = (CheckpointerShmem->num_requests >=
1179
1180 LWLockRelease(CheckpointerCommLock);
1181
1182 /* ... but not till after we release the lock */
1183 if (too_full)
1184 {
1185 volatile PROC_HDR *procglobal = ProcGlobal;
1186 ProcNumber checkpointerProc = procglobal->checkpointerProc;
1187
1188 if (checkpointerProc != INVALID_PROC_NUMBER)
1189 SetLatch(&GetPGProcByNumber(checkpointerProc)->procLatch);
1190 }
1191
1192 return true;
1193}
static bool CompactCheckpointerRequestQueue(void)
#define ERROR
Definition: elog.h:39
bool IsUnderPostmaster
Definition: globals.c:119
void SetLatch(Latch *latch)
Definition: latch.c:632
#define GetPGProcByNumber(n)
Definition: proc.h:423
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
Definition: proc.h:369
const char * type

References AmCheckpointerProcess, CheckpointerShmemStruct::checkpointer_pid, PROC_HDR::checkpointerProc, CheckpointerShmem, CompactCheckpointerRequestQueue(), elog, ERROR, CheckpointerRequest::ftag, GetPGProcByNumber, INVALID_PROC_NUMBER, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), CheckpointerShmemStruct::max_requests, CheckpointerShmemStruct::num_requests, ProcGlobal, CheckpointerShmemStruct::requests, SetLatch(), CheckpointerRequest::type, and type.

Referenced by RegisterSyncRequest().

◆ HandleCheckpointerInterrupts()

static void HandleCheckpointerInterrupts ( void  )
static

Definition at line 637 of file checkpointer.c.

638{
641
643 {
644 ConfigReloadPending = false;
646
647 /*
648 * Checkpointer is the last process to shut down, so we ask it to hold
649 * the keys for a range of other tasks required most of which have
650 * nothing to do with checkpointing at all.
651 *
652 * For various reasons, some config values can change dynamically so
653 * the primary copy of them is held in shared memory to make sure all
654 * backends see the same value. We make Checkpointer responsible for
655 * updating the shared memory copy if the parameter setting changes
656 * because of SIGHUP.
657 */
659 }
660
661 /* Perform logging of memory contexts of this process */
664}
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:40
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1289

References ConfigReloadPending, LogMemoryContextPending, PGC_SIGHUP, ProcessConfigFile(), ProcessLogMemoryContextInterrupt(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, and UpdateSharedMemoryConfig().

Referenced by CheckpointerMain().

◆ ImmediateCheckpointRequested()

static bool ImmediateCheckpointRequested ( void  )
static

Definition at line 740 of file checkpointer.c.

741{
743
744 /*
745 * We don't need to acquire the ckpt_lck in this case because we're only
746 * looking at a single flag bit.
747 */
749 return true;
750 return false;
751}

References CHECKPOINT_IMMEDIATE, CheckpointerShmem, and CheckpointerShmemStruct::ckpt_flags.

Referenced by CheckpointWriteDelay().

◆ IsCheckpointOnSchedule()

static bool IsCheckpointOnSchedule ( double  progress)
static

Definition at line 837 of file checkpointer.c.

838{
839 XLogRecPtr recptr;
840 struct timeval now;
841 double elapsed_xlogs,
843
845
846 /* Scale progress according to checkpoint_completion_target. */
848
849 /*
850 * Check against the cached value first. Only do the more expensive
851 * calculations once we reach the target previously calculated. Since
852 * neither time or WAL insert pointer moves backwards, a freshly
853 * calculated value can only be greater than or equal to the cached value.
854 */
856 return false;
857
858 /*
859 * Check progress against WAL segments written and CheckPointSegments.
860 *
861 * We compare the current WAL insert location against the location
862 * computed before calling CreateCheckPoint. The code in XLogInsert that
863 * actually triggers a checkpoint when CheckPointSegments is exceeded
864 * compares against RedoRecPtr, so this is not completely accurate.
865 * However, it's good enough for our purposes, we're only calculating an
866 * estimate anyway.
867 *
868 * During recovery, we compare last replayed WAL record's location with
869 * the location computed before calling CreateRestartPoint. That maintains
870 * the same pacing as we have during checkpoints in normal operation, but
871 * we might exceed max_wal_size by a fair amount. That's because there can
872 * be a large gap between a checkpoint's redo-pointer and the checkpoint
873 * record itself, and we only start the restartpoint after we've seen the
874 * checkpoint record. (The gap is typically up to CheckPointSegments *
875 * checkpoint_completion_target where checkpoint_completion_target is the
876 * value that was in effect when the WAL was generated).
877 */
878 if (RecoveryInProgress())
879 recptr = GetXLogReplayRecPtr(NULL);
880 else
881 recptr = GetInsertRecPtr();
882 elapsed_xlogs = (((double) (recptr - ckpt_start_recptr)) /
884
885 if (progress < elapsed_xlogs)
886 {
887 ckpt_cached_elapsed = elapsed_xlogs;
888 return false;
889 }
890
891 /*
892 * Check progress against time elapsed and checkpoint_timeout.
893 */
894 gettimeofday(&now, NULL);
895 elapsed_time = ((double) ((pg_time_t) now.tv_sec - ckpt_start_time) +
896 now.tv_usec / 1000000.0) / CheckPointTimeout;
897
899 {
901 return false;
902 }
903
904 /* It looks like we're on schedule. */
905 return true;
906}
double CheckPointCompletionTarget
Definition: checkpointer.c:142
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1312
int gettimeofday(struct timeval *tp, void *tzp)
int CheckPointSegments
Definition: xlog.c:156

References Assert, CheckPointCompletionTarget, CheckPointSegments, CheckPointTimeout, ckpt_active, ckpt_cached_elapsed, ckpt_start_recptr, ckpt_start_time, elapsed_time(), GetInsertRecPtr(), gettimeofday(), GetXLogReplayRecPtr(), now(), progress, RecoveryInProgress(), and wal_segment_size.

Referenced by CheckpointWriteDelay().

◆ ReqShutdownXLOG()

static void ReqShutdownXLOG ( SIGNAL_ARGS  )
static

Definition at line 916 of file checkpointer.c.

917{
918 ShutdownXLOGPending = true;
920}

References MyLatch, SetLatch(), and ShutdownXLOGPending.

Referenced by CheckpointerMain().

◆ RequestCheckpoint()

void RequestCheckpoint ( int  flags)

Definition at line 995 of file checkpointer.c.

996{
997 int ntries;
998 int old_failed,
999 old_started;
1000
1001 /*
1002 * If in a standalone backend, just do it ourselves.
1003 */
1005 {
1006 /*
1007 * There's no point in doing slow checkpoints in a standalone backend,
1008 * because there's no other backends the checkpoint could disrupt.
1009 */
1011
1012 /* Free all smgr objects, as CheckpointerMain() normally would. */
1014
1015 return;
1016 }
1017
1018 /*
1019 * Atomically set the request flags, and take a snapshot of the counters.
1020 * When we see ckpt_started > old_started, we know the flags we set here
1021 * have been seen by checkpointer.
1022 *
1023 * Note that we OR the flags with any existing flags, to avoid overriding
1024 * a "stronger" request by another backend. The flag senses must be
1025 * chosen to make this work!
1026 */
1028
1029 old_failed = CheckpointerShmem->ckpt_failed;
1030 old_started = CheckpointerShmem->ckpt_started;
1032
1034
1035 /*
1036 * Set checkpointer's latch to request checkpoint. It's possible that the
1037 * checkpointer hasn't started yet, so we will retry a few times if
1038 * needed. (Actually, more than a few times, since on slow or overloaded
1039 * buildfarm machines, it's been observed that the checkpointer can take
1040 * several seconds to start.) However, if not told to wait for the
1041 * checkpoint to occur, we consider failure to set the latch to be
1042 * nonfatal and merely LOG it. The checkpointer should see the request
1043 * when it does start, with or without the SetLatch().
1044 */
1045#define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
1046 for (ntries = 0;; ntries++)
1047 {
1048 volatile PROC_HDR *procglobal = ProcGlobal;
1049 ProcNumber checkpointerProc = procglobal->checkpointerProc;
1050
1051 if (checkpointerProc == INVALID_PROC_NUMBER)
1052 {
1053 if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
1054 {
1055 elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1056 "could not notify checkpoint: checkpointer is not running");
1057 break;
1058 }
1059 }
1060 else
1061 {
1062 SetLatch(&GetPGProcByNumber(checkpointerProc)->procLatch);
1063 /* notified successfully */
1064 break;
1065 }
1066
1068 pg_usleep(100000L); /* wait 0.1 sec, then retry */
1069 }
1070
1071 /*
1072 * If requested, wait for completion. We detect completion according to
1073 * the algorithm given above.
1074 */
1075 if (flags & CHECKPOINT_WAIT)
1076 {
1077 int new_started,
1078 new_failed;
1079
1080 /* Wait for a new checkpoint to start. */
1082 for (;;)
1083 {
1085 new_started = CheckpointerShmem->ckpt_started;
1087
1088 if (new_started != old_started)
1089 break;
1090
1092 WAIT_EVENT_CHECKPOINT_START);
1093 }
1095
1096 /*
1097 * We are waiting for ckpt_done >= new_started, in a modulo sense.
1098 */
1100 for (;;)
1101 {
1102 int new_done;
1103
1105 new_done = CheckpointerShmem->ckpt_done;
1106 new_failed = CheckpointerShmem->ckpt_failed;
1108
1109 if (new_done - new_started >= 0)
1110 break;
1111
1113 WAIT_EVENT_CHECKPOINT_DONE);
1114 }
1116
1117 if (new_failed != old_failed)
1118 ereport(ERROR,
1119 (errmsg("checkpoint request failed"),
1120 errhint("Consult recent messages in the server log for details.")));
1121 }
1122}
#define MAX_SIGNAL_TRIES
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
int errmsg(const char *fmt,...)
Definition: elog.c:1070
bool IsPostmasterEnvironment
Definition: globals.c:118
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define CHECKPOINT_REQUESTED
Definition: xlog.h:146
#define CHECKPOINT_WAIT
Definition: xlog.h:145

References CHECK_FOR_INTERRUPTS, CHECKPOINT_IMMEDIATE, CHECKPOINT_REQUESTED, CHECKPOINT_WAIT, PROC_HDR::checkpointerProc, CheckpointerShmem, CheckpointerShmemStruct::ckpt_done, CheckpointerShmemStruct::ckpt_failed, CheckpointerShmemStruct::ckpt_flags, CheckpointerShmemStruct::ckpt_lck, CheckpointerShmemStruct::ckpt_started, ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), CreateCheckPoint(), CheckpointerShmemStruct::done_cv, elog, ereport, errhint(), errmsg(), ERROR, GetPGProcByNumber, INVALID_PROC_NUMBER, IsPostmasterEnvironment, LOG, MAX_SIGNAL_TRIES, pg_usleep(), ProcGlobal, SetLatch(), smgrdestroyall(), SpinLockAcquire, SpinLockRelease, and CheckpointerShmemStruct::start_cv.

Referenced by CreateDatabaseUsingFileCopy(), do_pg_backup_start(), dropdb(), DropTableSpace(), movedb(), PerformRecoveryXLogAction(), standard_ProcessUtility(), StartupXLOG(), XLogPageRead(), and XLogWrite().

◆ UpdateSharedMemoryConfig()

static void UpdateSharedMemoryConfig ( void  )
static

Definition at line 1369 of file checkpointer.c.

1370{
1371 /* update global shmem state for sync rep */
1373
1374 /*
1375 * If full_page_writes has been changed by SIGHUP, we update it in shared
1376 * memory and write an XLOG_FPW_CHANGE record.
1377 */
1379
1380 elog(DEBUG2, "checkpointer updated shared memory configuration values");
1381}
#define DEBUG2
Definition: elog.h:29
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:921
void UpdateFullPageWrites(void)
Definition: xlog.c:8187

References DEBUG2, elog, SyncRepUpdateSyncStandbysDefined(), and UpdateFullPageWrites().

Referenced by CheckpointerMain(), CheckpointWriteDelay(), and HandleCheckpointerInterrupts().

Variable Documentation

◆ CheckPointCompletionTarget

double CheckPointCompletionTarget = 0.9

◆ CheckpointerShmem

◆ CheckPointTimeout

int CheckPointTimeout = 300

Definition at line 140 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ CheckPointWarning

int CheckPointWarning = 30

Definition at line 141 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ ckpt_active

bool ckpt_active = false
static

Definition at line 147 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_cached_elapsed

double ckpt_cached_elapsed
static

Definition at line 153 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_recptr

XLogRecPtr ckpt_start_recptr
static

Definition at line 152 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_time

pg_time_t ckpt_start_time
static

Definition at line 151 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ last_checkpoint_time

pg_time_t last_checkpoint_time
static

Definition at line 155 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ last_xlog_switch_time

pg_time_t last_xlog_switch_time
static

Definition at line 156 of file checkpointer.c.

Referenced by CheckArchiveTimeout(), and CheckpointerMain().

◆ ShutdownXLOGPending

volatile sig_atomic_t ShutdownXLOGPending = false
static

Definition at line 148 of file checkpointer.c.

Referenced by CheckpointerMain(), CheckpointWriteDelay(), and ReqShutdownXLOG().