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/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/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 ReqCheckpointHandler (SIGNAL_ARGS)
 
void CheckpointerMain (void)
 
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 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 139 of file checkpointer.c.

Function Documentation

◆ AbsorbSyncRequests()

void AbsorbSyncRequests ( void  )

Definition at line 1262 of file checkpointer.c.

1263 {
1264  CheckpointerRequest *requests = NULL;
1265  CheckpointerRequest *request;
1266  int n;
1267 
1268  if (!AmCheckpointerProcess())
1269  return;
1270 
1271  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1272 
1273  /* Transfer stats counts into pending pgstats message */
1278 
1281 
1282  /*
1283  * We try to avoid holding the lock for a long time by copying the request
1284  * array, and processing the requests after releasing the lock.
1285  *
1286  * Once we have cleared the requests from shared memory, we have to PANIC
1287  * if we then fail to absorb them (eg, because our hashtable runs out of
1288  * memory). This is because the system cannot run safely if we are unable
1289  * to fsync what we have been told to fsync. Fortunately, the hashtable
1290  * is so small that the problem is quite unlikely to arise in practice.
1291  */
1293  if (n > 0)
1294  {
1295  requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1296  memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1297  }
1298 
1300 
1302 
1303  LWLockRelease(CheckpointerCommLock);
1304 
1305  for (request = requests; n > 0; request++, n--)
1306  RememberSyncRequest(&request->ftag, request->type);
1307 
1308  END_CRIT_SECTION();
1309 
1310  if (requests)
1311  pfree(requests);
1312 }
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:136
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc(Size size)
Definition: mcxt.c:1226
#define AmCheckpointerProcess()
Definition: miscadmin.h:455
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
PgStat_CheckpointerStats PendingCheckpointerStats
SyncRequestType type
Definition: checkpointer.c:109
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:133
PgStat_Counter buf_written_backend
Definition: pgstat.h:268
PgStat_Counter buf_fsync_backend
Definition: pgstat.h:269
void RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
Definition: sync.c:492

References AmCheckpointerProcess, PgStat_CheckpointerStats::buf_fsync_backend, PgStat_CheckpointerStats::buf_written_backend, CheckpointerShmem, END_CRIT_SECTION, CheckpointerRequest::ftag, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), CheckpointerShmemStruct::num_backend_fsync, CheckpointerShmemStruct::num_backend_writes, CheckpointerShmemStruct::num_requests, palloc(), PendingCheckpointerStats, pfree(), RememberSyncRequest(), CheckpointerShmemStruct::requests, START_CRIT_SECTION, and CheckpointerRequest::type.

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

◆ CheckArchiveTimeout()

static void CheckArchiveTimeout ( void  )
static

Definition at line 608 of file checkpointer.c.

609 {
610  pg_time_t now;
611  pg_time_t last_time;
612  XLogRecPtr last_switch_lsn;
613 
615  return;
616 
617  now = (pg_time_t) time(NULL);
618 
619  /* First we do a quick check using possibly-stale local state. */
621  return;
622 
623  /*
624  * Update local state ... note that last_xlog_switch_time is the last time
625  * a switch was performed *or requested*.
626  */
627  last_time = GetLastSegSwitchData(&last_switch_lsn);
628 
630 
631  /* Now we can do the real checks */
633  {
634  /*
635  * Switch segment only when "important" WAL has been logged since the
636  * last segment switch (last_switch_lsn points to end of segment
637  * switch occurred in).
638  */
639  if (GetLastImportantRecPtr() > last_switch_lsn)
640  {
641  XLogRecPtr switchpoint;
642 
643  /* mark switch as unimportant, avoids triggering checkpoints */
644  switchpoint = RequestXLogSwitch(true);
645 
646  /*
647  * If the returned pointer points exactly to a segment boundary,
648  * assume nothing happened.
649  */
650  if (XLogSegmentOffset(switchpoint, wal_segment_size) != 0)
651  elog(DEBUG1, "write-ahead log switch forced (archive_timeout=%d)",
653  }
654 
655  /*
656  * Update state in any case, so we don't retry constantly when the
657  * system is idle.
658  */
660  }
661 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
#define Max(x, y)
Definition: c.h:987
static pg_time_t last_xlog_switch_time
Definition: checkpointer.c:159
#define DEBUG1
Definition: elog.h:30
int64 pg_time_t
Definition: pgtime.h:23
bool RecoveryInProgress(void)
Definition: xlog.c:5948
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:7568
int wal_segment_size
Definition: xlog.c:146
int XLogArchiveTimeout
Definition: xlog.c:121
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
Definition: xlog.c:6182
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:6153
#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 ( void  )

Definition at line 181 of file checkpointer.c.

182 {
183  sigjmp_buf local_sigjmp_buf;
184  MemoryContext checkpointer_context;
185 
187 
188  /*
189  * Properly accept or ignore signals the postmaster might send us
190  *
191  * Note: we deliberately ignore SIGTERM, because during a standard Unix
192  * system shutdown cycle, init will SIGTERM all processes at once. We
193  * want to wait for the backends to exit, whereupon the postmaster will
194  * tell us it's okay to shut down (via SIGUSR2).
195  */
197  pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
198  pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
199  /* SIGQUIT handler was already set up by InitPostmasterChild */
204 
205  /*
206  * Reset some signals that are accepted by postmaster but not here
207  */
209 
210  /*
211  * Initialize so that first time-driven event happens at the correct time.
212  */
214 
215  /*
216  * Write out stats after shutdown. This needs to be called by exactly one
217  * process during a normal shutdown, and since checkpointer is shut down
218  * very late...
219  *
220  * Walsenders are shut down after the checkpointer, but currently don't
221  * report stats. If that changes, we need a more complicated solution.
222  */
224 
225  /*
226  * Create a memory context that we will do all our work in. We do this so
227  * that we can reset the context during error recovery and thereby avoid
228  * possible memory leaks. Formerly this code just ran in
229  * TopMemoryContext, but resetting that would be a really bad idea.
230  */
231  checkpointer_context = AllocSetContextCreate(TopMemoryContext,
232  "Checkpointer",
234  MemoryContextSwitchTo(checkpointer_context);
235 
236  /*
237  * If an exception is encountered, processing resumes here.
238  *
239  * You might wonder why this isn't coded as an infinite loop around a
240  * PG_TRY construct. The reason is that this is the bottom of the
241  * exception stack, and so with PG_TRY there would be no exception handler
242  * in force at all during the CATCH part. By leaving the outermost setjmp
243  * always active, we have at least some chance of recovering from an error
244  * during error recovery. (If we get into an infinite loop thereby, it
245  * will soon be stopped by overflow of elog.c's internal state stack.)
246  *
247  * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
248  * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
249  * signals other than SIGQUIT will be blocked until we complete error
250  * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
251  * call redundant, but it is not since InterruptPending might be set
252  * already.
253  */
254  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
255  {
256  /* Since not using PG_TRY, must reset error stack by hand */
257  error_context_stack = NULL;
258 
259  /* Prevent interrupts while cleaning up */
260  HOLD_INTERRUPTS();
261 
262  /* Report the error to the server log */
263  EmitErrorReport();
264 
265  /*
266  * These operations are really just a minimal subset of
267  * AbortTransaction(). We don't have very many resources to worry
268  * about in checkpointer, but we do have LWLocks, buffers, and temp
269  * files.
270  */
274  UnlockBuffers();
276  AtEOXact_Buffers(false);
277  AtEOXact_SMgr();
278  AtEOXact_Files(false);
279  AtEOXact_HashTables(false);
280 
281  /* Warn any waiting backends that the checkpoint failed. */
282  if (ckpt_active)
283  {
288 
290 
291  ckpt_active = false;
292  }
293 
294  /*
295  * Now return to normal top-level context and clear ErrorContext for
296  * next time.
297  */
298  MemoryContextSwitchTo(checkpointer_context);
299  FlushErrorState();
300 
301  /* Flush any leaked data in the top-level context */
302  MemoryContextResetAndDeleteChildren(checkpointer_context);
303 
304  /* Now we can allow interrupts again */
306 
307  /*
308  * Sleep at least 1 second after any error. A write error is likely
309  * to be repeated, and we don't want to be filling the error logs as
310  * fast as we can.
311  */
312  pg_usleep(1000000L);
313 
314  /*
315  * Close all open files after any error. This is helpful on Windows,
316  * where holding deleted files open causes various strange errors.
317  * It's not clear we need it elsewhere, but shouldn't hurt.
318  */
319  smgrcloseall();
320  }
321 
322  /* We can now handle ereport(ERROR) */
323  PG_exception_stack = &local_sigjmp_buf;
324 
325  /*
326  * Unblock signals (they were blocked when the postmaster forked us)
327  */
328  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
329 
330  /*
331  * Ensure all shared memory values are set correctly for the config. Doing
332  * this here ensures no race conditions from other concurrent updaters.
333  */
335 
336  /*
337  * Advertise our latch that backends can use to wake us up while we're
338  * sleeping.
339  */
341 
342  /*
343  * Loop forever
344  */
345  for (;;)
346  {
347  bool do_checkpoint = false;
348  int flags = 0;
349  pg_time_t now;
350  int elapsed_secs;
351  int cur_timeout;
352 
353  /* Clear any already-pending wakeups */
355 
356  /*
357  * Process any requests or signals received recently.
358  */
361 
362  /*
363  * Detect a pending checkpoint request by checking whether the flags
364  * word in shared memory is nonzero. We shouldn't need to acquire the
365  * ckpt_lck for this.
366  */
367  if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
368  {
369  do_checkpoint = true;
371  }
372 
373  /*
374  * Force a checkpoint if too much time has elapsed since the last one.
375  * Note that we count a timed checkpoint in stats only when this
376  * occurs without an external request, but we set the CAUSE_TIME flag
377  * bit even if there is also an external request.
378  */
379  now = (pg_time_t) time(NULL);
380  elapsed_secs = now - last_checkpoint_time;
381  if (elapsed_secs >= CheckPointTimeout)
382  {
383  if (!do_checkpoint)
385  do_checkpoint = true;
386  flags |= CHECKPOINT_CAUSE_TIME;
387  }
388 
389  /*
390  * Do a checkpoint if requested.
391  */
392  if (do_checkpoint)
393  {
394  bool ckpt_performed = false;
395  bool do_restartpoint;
396 
397  /* Check if we should perform a checkpoint or a restartpoint. */
398  do_restartpoint = RecoveryInProgress();
399 
400  /*
401  * Atomically fetch the request flags to figure out what kind of a
402  * checkpoint we should perform, and increase the started-counter
403  * to acknowledge that we've started a new checkpoint.
404  */
406  flags |= CheckpointerShmem->ckpt_flags;
410 
412 
413  /*
414  * The end-of-recovery checkpoint is a real checkpoint that's
415  * performed while we're still in recovery.
416  */
417  if (flags & CHECKPOINT_END_OF_RECOVERY)
418  do_restartpoint = false;
419 
420  /*
421  * We will warn if (a) too soon since last checkpoint (whatever
422  * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
423  * since the last checkpoint start. Note in particular that this
424  * implementation will not generate warnings caused by
425  * CheckPointTimeout < CheckPointWarning.
426  */
427  if (!do_restartpoint &&
428  (flags & CHECKPOINT_CAUSE_XLOG) &&
429  elapsed_secs < CheckPointWarning)
430  ereport(LOG,
431  (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
432  "checkpoints are occurring too frequently (%d seconds apart)",
433  elapsed_secs,
434  elapsed_secs),
435  errhint("Consider increasing the configuration parameter \"max_wal_size\".")));
436 
437  /*
438  * Initialize checkpointer-private variables used during
439  * checkpoint.
440  */
441  ckpt_active = true;
442  if (do_restartpoint)
444  else
448 
449  /*
450  * Do the checkpoint.
451  */
452  if (!do_restartpoint)
453  {
454  CreateCheckPoint(flags);
455  ckpt_performed = true;
456  }
457  else
458  ckpt_performed = CreateRestartPoint(flags);
459 
460  /*
461  * After any checkpoint, close all smgr files. This is so we
462  * won't hang onto smgr references to deleted files indefinitely.
463  */
464  smgrcloseall();
465 
466  /*
467  * Indicate checkpoint completion to any waiting backends.
468  */
472 
474 
475  if (ckpt_performed)
476  {
477  /*
478  * Note we record the checkpoint start time not end time as
479  * last_checkpoint_time. This is so that time-driven
480  * checkpoints happen at a predictable spacing.
481  */
483  }
484  else
485  {
486  /*
487  * We were not able to perform the restartpoint (checkpoints
488  * throw an ERROR in case of error). Most likely because we
489  * have not received any new checkpoint WAL records since the
490  * last restartpoint. Try again in 15 s.
491  */
493  }
494 
495  ckpt_active = false;
496 
497  /* We may have received an interrupt during the checkpoint. */
499  }
500 
501  /* Check for archive_timeout and switch xlog files if necessary. */
503 
504  /* Report pending statistics to the cumulative stats system */
506  pgstat_report_wal(true);
507 
508  /*
509  * If any checkpoint flags have been set, redo the loop to handle the
510  * checkpoint without sleeping.
511  */
512  if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
513  continue;
514 
515  /*
516  * Sleep until we are signaled or it's time for another checkpoint or
517  * xlog file switch.
518  */
519  now = (pg_time_t) time(NULL);
520  elapsed_secs = now - last_checkpoint_time;
521  if (elapsed_secs >= CheckPointTimeout)
522  continue; /* no sleep for us ... */
523  cur_timeout = CheckPointTimeout - elapsed_secs;
525  {
526  elapsed_secs = now - last_xlog_switch_time;
527  if (elapsed_secs >= XLogArchiveTimeout)
528  continue; /* no sleep for us ... */
529  cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
530  }
531 
532  (void) WaitLatch(MyLatch,
534  cur_timeout * 1000L /* convert to ms */ ,
535  WAIT_EVENT_CHECKPOINTER_MAIN);
536  }
537 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3132
void UnlockBuffers(void)
Definition: bufmgr.c:4687
#define Min(x, y)
Definition: c.h:993
static void UpdateSharedMemoryConfig(void)
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:155
static void ReqCheckpointHandler(SIGNAL_ARGS)
Definition: checkpointer.c:844
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:608
static double ckpt_cached_elapsed
Definition: checkpointer.c:156
static bool ckpt_active
Definition: checkpointer.c:151
static void HandleCheckpointerInterrupts(void)
Definition: checkpointer.c:543
void AbsorbSyncRequests(void)
int CheckPointWarning
Definition: checkpointer.c:145
int CheckPointTimeout
Definition: checkpointer.c:144
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:158
static pg_time_t ckpt_start_time
Definition: checkpointer.c:154
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1878
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1179
void EmitErrorReport(void)
Definition: elog.c:1669
ErrorContextCallback * error_context_stack
Definition: elog.c:95
void FlushErrorState(void)
Definition: elog.c:1825
int errhint(const char *fmt,...)
Definition: elog.c:1316
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3110
int MyProcPid
Definition: globals.c:44
struct Latch * MyLatch
Definition: globals.c:58
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
void ResetLatch(Latch *latch)
Definition: latch.c:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
void LWLockReleaseAll(void)
Definition: lwlock.c:1903
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:70
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:134
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
void pgstat_before_server_shutdown(int code, Datum arg)
Definition: pgstat.c:465
void pgstat_report_checkpointer(void)
void pgstat_report_wal(bool force)
Definition: pgstat_wal.c:48
pqsigfunc pqsignal(int signo, pqsigfunc func)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:639
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:934
void pg_usleep(long microsec)
Definition: signal.c:53
void smgrcloseall(void)
Definition: smgr.c:327
void AtEOXact_SMgr(void)
Definition: smgr.c:739
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
ConditionVariable done_cv
Definition: checkpointer.c:126
ConditionVariable start_cv
Definition: checkpointer.c:125
Latch procLatch
Definition: proc.h:170
Latch * checkpointerLatch
Definition: proc.h:396
PgStat_Counter requested_checkpoints
Definition: pgstat.h:264
PgStat_Counter timed_checkpoints
Definition: pgstat.h:263
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGALRM
Definition: win32_port.h:174
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
bool CreateRestartPoint(int flags)
Definition: xlog.c:7119
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:6096
void CreateCheckPoint(int flags)
Definition: xlog.c:6476
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:143
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:135
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:144
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References AbsorbSyncRequests(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), before_shmem_exit(), CheckArchiveTimeout(), CHECKPOINT_CAUSE_TIME, CHECKPOINT_CAUSE_XLOG, CHECKPOINT_END_OF_RECOVERY, CheckpointerShmemStruct::checkpointer_pid, PROC_HDR::checkpointerLatch, 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, FlushErrorState(), GetInsertRecPtr(), GetXLogReplayRecPtr(), HandleCheckpointerInterrupts(), HOLD_INTERRUPTS, last_checkpoint_time, last_xlog_switch_time, LOG, LWLockReleaseAll(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), Min, MyLatch, MyProc, MyProcPid, now(), PendingCheckpointerStats, PG_exception_stack, pg_usleep(), pgstat_before_server_shutdown(), pgstat_report_checkpointer(), pgstat_report_wait_end(), pgstat_report_wal(), pqsignal(), ProcGlobal, PGPROC::procLatch, procsignal_sigusr1_handler(), RecoveryInProgress(), ReleaseAuxProcessResources(), ReqCheckpointHandler(), PgStat_CheckpointerStats::requested_checkpoints, ResetLatch(), RESUME_INTERRUPTS, SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, smgrcloseall(), SpinLockAcquire, SpinLockRelease, CheckpointerShmemStruct::start_cv, PgStat_CheckpointerStats::timed_checkpoints, TopMemoryContext, UnBlockSig, UnlockBuffers(), UpdateSharedMemoryConfig(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and XLogArchiveTimeout.

Referenced by AuxiliaryProcessMain().

◆ CheckpointerShmemInit()

void CheckpointerShmemInit ( void  )

Definition at line 887 of file checkpointer.c.

888 {
889  Size size = CheckpointerShmemSize();
890  bool found;
891 
893  ShmemInitStruct("Checkpointer Data",
894  size,
895  &found);
896 
897  if (!found)
898  {
899  /*
900  * First time through, so initialize. Note that we zero the whole
901  * requests array; this is so that CompactCheckpointerRequestQueue can
902  * assume that any pad bytes in the request structs are zeroes.
903  */
904  MemSet(CheckpointerShmem, 0, size);
909  }
910 }
#define MemSet(start, val, len)
Definition: c.h:1009
size_t Size
Definition: c.h:594
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:868
void ConditionVariableInit(ConditionVariable *cv)
int NBuffers
Definition: globals.c:136
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define SpinLockInit(lock)
Definition: spin.h:60

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ CheckpointerShmemSize()

Size CheckpointerShmemSize ( void  )

Definition at line 868 of file checkpointer.c.

869 {
870  Size size;
871 
872  /*
873  * Currently, the size of the requests[] array is arbitrarily set equal to
874  * NBuffers. This may prove too large or small ...
875  */
876  size = offsetof(CheckpointerShmemStruct, requests);
877  size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
878 
879  return size;
880 }
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

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

Referenced by CalculateShmemSize(), and CheckpointerShmemInit().

◆ CheckpointWriteDelay()

void CheckpointWriteDelay ( int  flags,
double  progress 
)

Definition at line 696 of file checkpointer.c.

697 {
698  static int absorb_counter = WRITES_PER_ABSORB;
699 
700  /* Do nothing if checkpoint is being executed by non-checkpointer process */
701  if (!AmCheckpointerProcess())
702  return;
703 
704  /*
705  * Perform the usual duties and take a nap, unless we're behind schedule,
706  * in which case we just try to catch up as quickly as possible.
707  */
708  if (!(flags & CHECKPOINT_IMMEDIATE) &&
712  {
714  {
715  ConfigReloadPending = false;
717  /* update shmem copies of config variables */
719  }
720 
722  absorb_counter = WRITES_PER_ABSORB;
723 
725 
726  /* Report interim statistics to the cumulative stats system */
728 
729  /*
730  * This sleep used to be connected to bgwriter_delay, typically 200ms.
731  * That resulted in more frequent wakeups if not much work to do.
732  * Checkpointer and bgwriter are no longer related so take the Big
733  * Sleep.
734  */
736  100,
737  WAIT_EVENT_CHECKPOINT_WRITE_DELAY);
739  }
740  else if (--absorb_counter <= 0)
741  {
742  /*
743  * Absorb pending fsync requests after each WRITES_PER_ABSORB write
744  * operations even when we don't sleep, to prevent overflow of the
745  * fsync request queue.
746  */
748  absorb_counter = WRITES_PER_ABSORB;
749  }
750 
751  /* Check for barrier events. */
754 }
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:669
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:765
#define WRITES_PER_ABSORB
Definition: checkpointer.c:139
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
int progress
Definition: pgbench.c:261
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:468
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:136

References AbsorbSyncRequests(), AmCheckpointerProcess, CheckArchiveTimeout(), CHECKPOINT_IMMEDIATE, ConfigReloadPending, ImmediateCheckpointRequested(), IsCheckpointOnSchedule(), MyLatch, PGC_SIGHUP, pgstat_report_checkpointer(), ProcessConfigFile(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, progress, ResetLatch(), ShutdownRequestPending, 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 1156 of file checkpointer.c.

1157 {
1158  struct CheckpointerSlotMapping
1159  {
1160  CheckpointerRequest request;
1161  int slot;
1162  };
1163 
1164  int n,
1165  preserve_count;
1166  int num_skipped = 0;
1167  HASHCTL ctl;
1168  HTAB *htab;
1169  bool *skip_slot;
1170 
1171  /* must hold CheckpointerCommLock in exclusive mode */
1172  Assert(LWLockHeldByMe(CheckpointerCommLock));
1173 
1174  /* Initialize skip_slot array */
1175  skip_slot = palloc0(sizeof(bool) * CheckpointerShmem->num_requests);
1176 
1177  /* Initialize temporary hash table */
1178  ctl.keysize = sizeof(CheckpointerRequest);
1179  ctl.entrysize = sizeof(struct CheckpointerSlotMapping);
1180  ctl.hcxt = CurrentMemoryContext;
1181 
1182  htab = hash_create("CompactCheckpointerRequestQueue",
1184  &ctl,
1186 
1187  /*
1188  * The basic idea here is that a request can be skipped if it's followed
1189  * by a later, identical request. It might seem more sensible to work
1190  * backwards from the end of the queue and check whether a request is
1191  * *preceded* by an earlier, identical request, in the hopes of doing less
1192  * copying. But that might change the semantics, if there's an
1193  * intervening SYNC_FORGET_REQUEST or SYNC_FILTER_REQUEST, so we do it
1194  * this way. It would be possible to be even smarter if we made the code
1195  * below understand the specific semantics of such requests (it could blow
1196  * away preceding entries that would end up being canceled anyhow), but
1197  * it's not clear that the extra complexity would buy us anything.
1198  */
1199  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1200  {
1201  CheckpointerRequest *request;
1202  struct CheckpointerSlotMapping *slotmap;
1203  bool found;
1204 
1205  /*
1206  * We use the request struct directly as a hashtable key. This
1207  * assumes that any padding bytes in the structs are consistently the
1208  * same, which should be okay because we zeroed them in
1209  * CheckpointerShmemInit. Note also that RelFileLocator had better
1210  * contain no pad bytes.
1211  */
1212  request = &CheckpointerShmem->requests[n];
1213  slotmap = hash_search(htab, request, HASH_ENTER, &found);
1214  if (found)
1215  {
1216  /* Duplicate, so mark the previous occurrence as skippable */
1217  skip_slot[slotmap->slot] = true;
1218  num_skipped++;
1219  }
1220  /* Remember slot containing latest occurrence of this request value */
1221  slotmap->slot = n;
1222  }
1223 
1224  /* Done with the hash table. */
1225  hash_destroy(htab);
1226 
1227  /* If no duplicates, we're out of luck. */
1228  if (!num_skipped)
1229  {
1230  pfree(skip_slot);
1231  return false;
1232  }
1233 
1234  /* We found some duplicates; remove them. */
1235  preserve_count = 0;
1236  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1237  {
1238  if (skip_slot[n])
1239  continue;
1240  CheckpointerShmem->requests[preserve_count++] = CheckpointerShmem->requests[n];
1241  }
1242  ereport(DEBUG1,
1243  (errmsg_internal("compacted fsync request queue from %d entries to %d entries",
1244  CheckpointerShmem->num_requests, preserve_count)));
1245  CheckpointerShmem->num_requests = preserve_count;
1246 
1247  /* Cleanup. */
1248  pfree(skip_slot);
1249  return true;
1250 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:863
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
@ 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
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1920
void * palloc0(Size size)
Definition: mcxt.c:1257
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220

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

Referenced by ForwardSyncRequest().

◆ FirstCallSinceLastCheckpoint()

bool FirstCallSinceLastCheckpoint ( void  )

Definition at line 1337 of file checkpointer.c.

1338 {
1339  static int ckpt_done = 0;
1340  int new_done;
1341  bool FirstCall = false;
1342 
1344  new_done = CheckpointerShmem->ckpt_done;
1346 
1347  if (new_done != ckpt_done)
1348  FirstCall = true;
1349 
1350  ckpt_done = new_done;
1351 
1352  return FirstCall;
1353 }

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 1085 of file checkpointer.c.

1086 {
1087  CheckpointerRequest *request;
1088  bool too_full;
1089 
1090  if (!IsUnderPostmaster)
1091  return false; /* probably shouldn't even get here */
1092 
1093  if (AmCheckpointerProcess())
1094  elog(ERROR, "ForwardSyncRequest must not be called in checkpointer");
1095 
1096  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1097 
1098  /* Count all backend writes regardless of if they fit in the queue */
1101 
1102  /*
1103  * If the checkpointer isn't running or the request queue is full, the
1104  * backend will have to perform its own fsync request. But before forcing
1105  * that to happen, we can try to compact the request queue.
1106  */
1107  if (CheckpointerShmem->checkpointer_pid == 0 ||
1110  {
1111  /*
1112  * Count the subset of writes where backends have to do their own
1113  * fsync
1114  */
1117  LWLockRelease(CheckpointerCommLock);
1118  return false;
1119  }
1120 
1121  /* OK, insert request */
1123  request->ftag = *ftag;
1124  request->type = type;
1125 
1126  /* If queue is more than half full, nudge the checkpointer to empty it */
1127  too_full = (CheckpointerShmem->num_requests >=
1129 
1130  LWLockRelease(CheckpointerCommLock);
1131 
1132  /* ... but not till after we release the lock */
1133  if (too_full && ProcGlobal->checkpointerLatch)
1135 
1136  return true;
1137 }
static bool CompactCheckpointerRequestQueue(void)
#define ERROR
Definition: elog.h:39
bool IsUnderPostmaster
Definition: globals.c:113
void SetLatch(Latch *latch)
Definition: latch.c:605
#define AmBackgroundWriterProcess()
Definition: miscadmin.h:453
const char * type

References AmBackgroundWriterProcess, AmCheckpointerProcess, CheckpointerShmemStruct::checkpointer_pid, PROC_HDR::checkpointerLatch, CheckpointerShmem, CompactCheckpointerRequestQueue(), elog(), ERROR, CheckpointerRequest::ftag, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), CheckpointerShmemStruct::max_requests, CheckpointerShmemStruct::num_backend_fsync, CheckpointerShmemStruct::num_backend_writes, CheckpointerShmemStruct::num_requests, ProcGlobal, CheckpointerShmemStruct::requests, SetLatch(), CheckpointerRequest::type, and type.

Referenced by RegisterSyncRequest().

◆ HandleCheckpointerInterrupts()

static void HandleCheckpointerInterrupts ( void  )
static

Definition at line 543 of file checkpointer.c.

544 {
547 
549  {
550  ConfigReloadPending = false;
552 
553  /*
554  * Checkpointer is the last process to shut down, so we ask it to hold
555  * the keys for a range of other tasks required most of which have
556  * nothing to do with checkpointing at all.
557  *
558  * For various reasons, some config values can change dynamically so
559  * the primary copy of them is held in shared memory to make sure all
560  * backends see the same value. We make Checkpointer responsible for
561  * updating the shared memory copy if the parameter setting changes
562  * because of SIGHUP.
563  */
565  }
567  {
568  /*
569  * From here on, elog(ERROR) should end with exit(1), not send control
570  * back to the sigsetjmp block above
571  */
572  ExitOnAnyError = true;
573 
574  /*
575  * Close down the database.
576  *
577  * Since ShutdownXLOG() creates restartpoint or checkpoint, and
578  * updates the statistics, increment the checkpoint request and flush
579  * out pending statistic.
580  */
582  ShutdownXLOG(0, 0);
584  pgstat_report_wal(true);
585 
586  /* Normal exit from the checkpointer is here */
587  proc_exit(0); /* done */
588  }
589 
590  /* Perform logging of memory contexts of this process */
593 }
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
bool ExitOnAnyError
Definition: globals.c:117
void proc_exit(int code)
Definition: ipc.c:104
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1199
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:6199

References ConfigReloadPending, ExitOnAnyError, LogMemoryContextPending, PendingCheckpointerStats, PGC_SIGHUP, pgstat_report_checkpointer(), pgstat_report_wal(), proc_exit(), ProcessConfigFile(), ProcessLogMemoryContextInterrupt(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, PgStat_CheckpointerStats::requested_checkpoints, ShutdownRequestPending, ShutdownXLOG(), and UpdateSharedMemoryConfig().

Referenced by CheckpointerMain().

◆ ImmediateCheckpointRequested()

static bool ImmediateCheckpointRequested ( void  )
static

Definition at line 669 of file checkpointer.c.

670 {
672 
673  /*
674  * We don't need to acquire the ckpt_lck in this case because we're only
675  * looking at a single flag bit.
676  */
677  if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
678  return true;
679  return false;
680 }

References CHECKPOINT_IMMEDIATE, CheckpointerShmem, and CheckpointerShmemStruct::ckpt_flags.

Referenced by CheckpointWriteDelay().

◆ IsCheckpointOnSchedule()

static bool IsCheckpointOnSchedule ( double  progress)
static

Definition at line 765 of file checkpointer.c.

766 {
767  XLogRecPtr recptr;
768  struct timeval now;
769  double elapsed_xlogs,
770  elapsed_time;
771 
773 
774  /* Scale progress according to checkpoint_completion_target. */
776 
777  /*
778  * Check against the cached value first. Only do the more expensive
779  * calculations once we reach the target previously calculated. Since
780  * neither time or WAL insert pointer moves backwards, a freshly
781  * calculated value can only be greater than or equal to the cached value.
782  */
784  return false;
785 
786  /*
787  * Check progress against WAL segments written and CheckPointSegments.
788  *
789  * We compare the current WAL insert location against the location
790  * computed before calling CreateCheckPoint. The code in XLogInsert that
791  * actually triggers a checkpoint when CheckPointSegments is exceeded
792  * compares against RedoRecPtr, so this is not completely accurate.
793  * However, it's good enough for our purposes, we're only calculating an
794  * estimate anyway.
795  *
796  * During recovery, we compare last replayed WAL record's location with
797  * the location computed before calling CreateRestartPoint. That maintains
798  * the same pacing as we have during checkpoints in normal operation, but
799  * we might exceed max_wal_size by a fair amount. That's because there can
800  * be a large gap between a checkpoint's redo-pointer and the checkpoint
801  * record itself, and we only start the restartpoint after we've seen the
802  * checkpoint record. (The gap is typically up to CheckPointSegments *
803  * checkpoint_completion_target where checkpoint_completion_target is the
804  * value that was in effect when the WAL was generated).
805  */
806  if (RecoveryInProgress())
807  recptr = GetXLogReplayRecPtr(NULL);
808  else
809  recptr = GetInsertRecPtr();
810  elapsed_xlogs = (((double) (recptr - ckpt_start_recptr)) /
812 
813  if (progress < elapsed_xlogs)
814  {
815  ckpt_cached_elapsed = elapsed_xlogs;
816  return false;
817  }
818 
819  /*
820  * Check progress against time elapsed and checkpoint_timeout.
821  */
822  gettimeofday(&now, NULL);
823  elapsed_time = ((double) ((pg_time_t) now.tv_sec - ckpt_start_time) +
824  now.tv_usec / 1000000.0) / CheckPointTimeout;
825 
826  if (progress < elapsed_time)
827  {
829  return false;
830  }
831 
832  /* It looks like we're on schedule. */
833  return true;
834 }
double CheckPointCompletionTarget
Definition: checkpointer.c:146
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1092
int gettimeofday(struct timeval *tp, void *tzp)
int CheckPointSegments
Definition: xlog.c:159

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().

◆ ReqCheckpointHandler()

static void ReqCheckpointHandler ( SIGNAL_ARGS  )
static

Definition at line 844 of file checkpointer.c.

845 {
846  int save_errno = errno;
847 
848  /*
849  * The signaling process should have set ckpt_flags nonzero, so all we
850  * need do is ensure that our main loop gets kicked out of any wait.
851  */
852  SetLatch(MyLatch);
853 
854  errno = save_errno;
855 }

References MyLatch, and SetLatch().

Referenced by CheckpointerMain().

◆ RequestCheckpoint()

void RequestCheckpoint ( int  flags)

Definition at line 930 of file checkpointer.c.

931 {
932  int ntries;
933  int old_failed,
934  old_started;
935 
936  /*
937  * If in a standalone backend, just do it ourselves.
938  */
940  {
941  /*
942  * There's no point in doing slow checkpoints in a standalone backend,
943  * because there's no other backends the checkpoint could disrupt.
944  */
946 
947  /*
948  * After any checkpoint, close all smgr files. This is so we won't
949  * hang onto smgr references to deleted files indefinitely.
950  */
951  smgrcloseall();
952 
953  return;
954  }
955 
956  /*
957  * Atomically set the request flags, and take a snapshot of the counters.
958  * When we see ckpt_started > old_started, we know the flags we set here
959  * have been seen by checkpointer.
960  *
961  * Note that we OR the flags with any existing flags, to avoid overriding
962  * a "stronger" request by another backend. The flag senses must be
963  * chosen to make this work!
964  */
966 
967  old_failed = CheckpointerShmem->ckpt_failed;
968  old_started = CheckpointerShmem->ckpt_started;
970 
972 
973  /*
974  * Send signal to request checkpoint. It's possible that the checkpointer
975  * hasn't started yet, or is in process of restarting, so we will retry a
976  * few times if needed. (Actually, more than a few times, since on slow
977  * or overloaded buildfarm machines, it's been observed that the
978  * checkpointer can take several seconds to start.) However, if not told
979  * to wait for the checkpoint to occur, we consider failure to send the
980  * signal to be nonfatal and merely LOG it. The checkpointer should see
981  * the request when it does start, with or without getting a signal.
982  */
983 #define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
984  for (ntries = 0;; ntries++)
985  {
987  {
988  if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
989  {
990  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
991  "could not signal for checkpoint: checkpointer is not running");
992  break;
993  }
994  }
995  else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
996  {
997  if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
998  {
999  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1000  "could not signal for checkpoint: %m");
1001  break;
1002  }
1003  }
1004  else
1005  break; /* signal sent successfully */
1006 
1008  pg_usleep(100000L); /* wait 0.1 sec, then retry */
1009  }
1010 
1011  /*
1012  * If requested, wait for completion. We detect completion according to
1013  * the algorithm given above.
1014  */
1015  if (flags & CHECKPOINT_WAIT)
1016  {
1017  int new_started,
1018  new_failed;
1019 
1020  /* Wait for a new checkpoint to start. */
1022  for (;;)
1023  {
1025  new_started = CheckpointerShmem->ckpt_started;
1027 
1028  if (new_started != old_started)
1029  break;
1030 
1032  WAIT_EVENT_CHECKPOINT_START);
1033  }
1035 
1036  /*
1037  * We are waiting for ckpt_done >= new_started, in a modulo sense.
1038  */
1040  for (;;)
1041  {
1042  int new_done;
1043 
1045  new_done = CheckpointerShmem->ckpt_done;
1046  new_failed = CheckpointerShmem->ckpt_failed;
1048 
1049  if (new_done - new_started >= 0)
1050  break;
1051 
1053  WAIT_EVENT_CHECKPOINT_DONE);
1054  }
1056 
1057  if (new_failed != old_failed)
1058  ereport(ERROR,
1059  (errmsg("checkpoint request failed"),
1060  errhint("Consult recent messages in the server log for details.")));
1061  }
1062 }
#define MAX_SIGNAL_TRIES
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
int errmsg(const char *fmt,...)
Definition: elog.c:1069
bool IsPostmasterEnvironment
Definition: globals.c:112
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define kill(pid, sig)
Definition: win32_port.h:485
#define CHECKPOINT_REQUESTED
Definition: xlog.h:141
#define CHECKPOINT_WAIT
Definition: xlog.h:140

References CHECK_FOR_INTERRUPTS, CHECKPOINT_IMMEDIATE, CHECKPOINT_REQUESTED, CHECKPOINT_WAIT, CheckpointerShmemStruct::checkpointer_pid, 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, IsPostmasterEnvironment, kill, LOG, MAX_SIGNAL_TRIES, pg_usleep(), smgrcloseall(), 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 1318 of file checkpointer.c.

1319 {
1320  /* update global shmem state for sync rep */
1322 
1323  /*
1324  * If full_page_writes has been changed by SIGHUP, we update it in shared
1325  * memory and write an XLOG_FPW_CHANGE record.
1326  */
1328 
1329  elog(DEBUG2, "checkpointer updated shared memory configuration values");
1330 }
#define DEBUG2
Definition: elog.h:29
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:927
void UpdateFullPageWrites(void)
Definition: xlog.c:7674

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 144 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ CheckPointWarning

int CheckPointWarning = 30

Definition at line 145 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ ckpt_active

bool ckpt_active = false
static

Definition at line 151 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_cached_elapsed

double ckpt_cached_elapsed
static

Definition at line 156 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_recptr

XLogRecPtr ckpt_start_recptr
static

Definition at line 155 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_time

pg_time_t ckpt_start_time
static

Definition at line 154 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ last_checkpoint_time

pg_time_t last_checkpoint_time
static

Definition at line 158 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ last_xlog_switch_time

pg_time_t last_xlog_switch_time
static

Definition at line 159 of file checkpointer.c.

Referenced by CheckArchiveTimeout(), and CheckpointerMain().