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

Function Documentation

◆ AbsorbSyncRequests()

void AbsorbSyncRequests ( void  )

Definition at line 1254 of file checkpointer.c.

1255 {
1256  CheckpointerRequest *requests = NULL;
1257  CheckpointerRequest *request;
1258  int n;
1259 
1260  if (!AmCheckpointerProcess())
1261  return;
1262 
1263  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1264 
1265  /*
1266  * We try to avoid holding the lock for a long time by copying the request
1267  * array, and processing the requests after releasing the lock.
1268  *
1269  * Once we have cleared the requests from shared memory, we have to PANIC
1270  * if we then fail to absorb them (eg, because our hashtable runs out of
1271  * memory). This is because the system cannot run safely if we are unable
1272  * to fsync what we have been told to fsync. Fortunately, the hashtable
1273  * is so small that the problem is quite unlikely to arise in practice.
1274  */
1276  if (n > 0)
1277  {
1278  requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1279  memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1280  }
1281 
1283 
1285 
1286  LWLockRelease(CheckpointerCommLock);
1287 
1288  for (request = requests; n > 0; request++, n--)
1289  RememberSyncRequest(&request->ftag, request->type);
1290 
1291  END_CRIT_SECTION();
1292 
1293  if (requests)
1294  pfree(requests);
1295 }
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pfree(void *pointer)
Definition: mcxt.c:1431
void * palloc(Size size)
Definition: mcxt.c:1201
#define AmCheckpointerProcess()
Definition: miscadmin.h:458
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
SyncRequestType type
Definition: checkpointer.c:103
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:124
void RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
Definition: sync.c:492

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(), ProcessSyncRequests(), SyncPostCheckpoint(), and SyncPreCheckpoint().

◆ CheckArchiveTimeout()

static void CheckArchiveTimeout ( void  )
static

Definition at line 617 of file checkpointer.c.

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

173 {
174  sigjmp_buf local_sigjmp_buf;
175  MemoryContext checkpointer_context;
176 
178 
179  /*
180  * Properly accept or ignore signals the postmaster might send us
181  *
182  * Note: we deliberately ignore SIGTERM, because during a standard Unix
183  * system shutdown cycle, init will SIGTERM all processes at once. We
184  * want to wait for the backends to exit, whereupon the postmaster will
185  * tell us it's okay to shut down (via SIGUSR2).
186  */
188  pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
189  pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
190  /* SIGQUIT handler was already set up by InitPostmasterChild */
195 
196  /*
197  * Reset some signals that are accepted by postmaster but not here
198  */
200 
201  /*
202  * Initialize so that first time-driven event happens at the correct time.
203  */
205 
206  /*
207  * Write out stats after shutdown. This needs to be called by exactly one
208  * process during a normal shutdown, and since checkpointer is shut down
209  * very late...
210  *
211  * Walsenders are shut down after the checkpointer, but currently don't
212  * report stats. If that changes, we need a more complicated solution.
213  */
215 
216  /*
217  * Create a memory context that we will do all our work in. We do this so
218  * that we can reset the context during error recovery and thereby avoid
219  * possible memory leaks. Formerly this code just ran in
220  * TopMemoryContext, but resetting that would be a really bad idea.
221  */
222  checkpointer_context = AllocSetContextCreate(TopMemoryContext,
223  "Checkpointer",
225  MemoryContextSwitchTo(checkpointer_context);
226 
227  /*
228  * If an exception is encountered, processing resumes here.
229  *
230  * You might wonder why this isn't coded as an infinite loop around a
231  * PG_TRY construct. The reason is that this is the bottom of the
232  * exception stack, and so with PG_TRY there would be no exception handler
233  * in force at all during the CATCH part. By leaving the outermost setjmp
234  * always active, we have at least some chance of recovering from an error
235  * during error recovery. (If we get into an infinite loop thereby, it
236  * will soon be stopped by overflow of elog.c's internal state stack.)
237  *
238  * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
239  * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
240  * signals other than SIGQUIT will be blocked until we complete error
241  * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
242  * call redundant, but it is not since InterruptPending might be set
243  * already.
244  */
245  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
246  {
247  /* Since not using PG_TRY, must reset error stack by hand */
248  error_context_stack = NULL;
249 
250  /* Prevent interrupts while cleaning up */
251  HOLD_INTERRUPTS();
252 
253  /* Report the error to the server log */
254  EmitErrorReport();
255 
256  /*
257  * These operations are really just a minimal subset of
258  * AbortTransaction(). We don't have very many resources to worry
259  * about in checkpointer, but we do have LWLocks, buffers, and temp
260  * files.
261  */
265  UnlockBuffers();
267  AtEOXact_Buffers(false);
268  AtEOXact_SMgr();
269  AtEOXact_Files(false);
270  AtEOXact_HashTables(false);
271 
272  /* Warn any waiting backends that the checkpoint failed. */
273  if (ckpt_active)
274  {
279 
281 
282  ckpt_active = false;
283  }
284 
285  /*
286  * Now return to normal top-level context and clear ErrorContext for
287  * next time.
288  */
289  MemoryContextSwitchTo(checkpointer_context);
290  FlushErrorState();
291 
292  /* Flush any leaked data in the top-level context */
293  MemoryContextReset(checkpointer_context);
294 
295  /* Now we can allow interrupts again */
297 
298  /*
299  * Sleep at least 1 second after any error. A write error is likely
300  * to be repeated, and we don't want to be filling the error logs as
301  * fast as we can.
302  */
303  pg_usleep(1000000L);
304  }
305 
306  /* We can now handle ereport(ERROR) */
307  PG_exception_stack = &local_sigjmp_buf;
308 
309  /*
310  * Unblock signals (they were blocked when the postmaster forked us)
311  */
312  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
313 
314  /*
315  * Ensure all shared memory values are set correctly for the config. Doing
316  * this here ensures no race conditions from other concurrent updaters.
317  */
319 
320  /*
321  * Advertise our latch that backends can use to wake us up while we're
322  * sleeping.
323  */
325 
326  /*
327  * Loop forever
328  */
329  for (;;)
330  {
331  bool do_checkpoint = false;
332  int flags = 0;
333  pg_time_t now;
334  int elapsed_secs;
335  int cur_timeout;
336  bool chkpt_or_rstpt_requested = false;
337  bool chkpt_or_rstpt_timed = false;
338 
339  /* Clear any already-pending wakeups */
341 
342  /*
343  * Process any requests or signals received recently.
344  */
347 
348  /*
349  * Detect a pending checkpoint request by checking whether the flags
350  * word in shared memory is nonzero. We shouldn't need to acquire the
351  * ckpt_lck for this.
352  */
353  if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
354  {
355  do_checkpoint = true;
356  chkpt_or_rstpt_requested = true;
357  }
358 
359  /*
360  * Force a checkpoint if too much time has elapsed since the last one.
361  * Note that we count a timed checkpoint in stats only when this
362  * occurs without an external request, but we set the CAUSE_TIME flag
363  * bit even if there is also an external request.
364  */
365  now = (pg_time_t) time(NULL);
366  elapsed_secs = now - last_checkpoint_time;
367  if (elapsed_secs >= CheckPointTimeout)
368  {
369  if (!do_checkpoint)
370  chkpt_or_rstpt_timed = true;
371  do_checkpoint = true;
372  flags |= CHECKPOINT_CAUSE_TIME;
373  }
374 
375  /*
376  * Do a checkpoint if requested.
377  */
378  if (do_checkpoint)
379  {
380  bool ckpt_performed = false;
381  bool do_restartpoint;
382 
383  /* Check if we should perform a checkpoint or a restartpoint. */
384  do_restartpoint = RecoveryInProgress();
385 
386  /*
387  * Atomically fetch the request flags to figure out what kind of a
388  * checkpoint we should perform, and increase the started-counter
389  * to acknowledge that we've started a new checkpoint.
390  */
392  flags |= CheckpointerShmem->ckpt_flags;
396 
398 
399  /*
400  * The end-of-recovery checkpoint is a real checkpoint that's
401  * performed while we're still in recovery.
402  */
403  if (flags & CHECKPOINT_END_OF_RECOVERY)
404  do_restartpoint = false;
405 
406  if (chkpt_or_rstpt_timed)
407  {
408  chkpt_or_rstpt_timed = false;
409  if (do_restartpoint)
411  else
413  }
414 
415  if (chkpt_or_rstpt_requested)
416  {
417  chkpt_or_rstpt_requested = false;
418  if (do_restartpoint)
420  else
422  }
423 
424  /*
425  * We will warn if (a) too soon since last checkpoint (whatever
426  * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
427  * since the last checkpoint start. Note in particular that this
428  * implementation will not generate warnings caused by
429  * CheckPointTimeout < CheckPointWarning.
430  */
431  if (!do_restartpoint &&
432  (flags & CHECKPOINT_CAUSE_XLOG) &&
433  elapsed_secs < CheckPointWarning)
434  ereport(LOG,
435  (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
436  "checkpoints are occurring too frequently (%d seconds apart)",
437  elapsed_secs,
438  elapsed_secs),
439  errhint("Consider increasing the configuration parameter max_wal_size.")));
440 
441  /*
442  * Initialize checkpointer-private variables used during
443  * checkpoint.
444  */
445  ckpt_active = true;
446  if (do_restartpoint)
448  else
452 
453  /*
454  * Do the checkpoint.
455  */
456  if (!do_restartpoint)
457  {
458  CreateCheckPoint(flags);
459  ckpt_performed = true;
460  }
461  else
462  ckpt_performed = CreateRestartPoint(flags);
463 
464  /*
465  * After any checkpoint, free all smgr objects. Otherwise we
466  * would never do so for dropped relations, as the checkpointer
467  * does not process shared invalidation messages or call
468  * AtEOXact_SMgr().
469  */
470  smgrdestroyall();
471 
472  /*
473  * Indicate checkpoint completion to any waiting backends.
474  */
478 
480 
481  if (ckpt_performed)
482  {
483  /*
484  * Note we record the checkpoint start time not end time as
485  * last_checkpoint_time. This is so that time-driven
486  * checkpoints happen at a predictable spacing.
487  */
489 
490  if (do_restartpoint)
492  }
493  else
494  {
495  /*
496  * We were not able to perform the restartpoint (checkpoints
497  * throw an ERROR in case of error). Most likely because we
498  * have not received any new checkpoint WAL records since the
499  * last restartpoint. Try again in 15 s.
500  */
502  }
503 
504  ckpt_active = false;
505 
506  /* We may have received an interrupt during the checkpoint. */
508  }
509 
510  /* Check for archive_timeout and switch xlog files if necessary. */
512 
513  /* Report pending statistics to the cumulative stats system */
515  pgstat_report_wal(true);
516 
517  /*
518  * If any checkpoint flags have been set, redo the loop to handle the
519  * checkpoint without sleeping.
520  */
521  if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
522  continue;
523 
524  /*
525  * Sleep until we are signaled or it's time for another checkpoint or
526  * xlog file switch.
527  */
528  now = (pg_time_t) time(NULL);
529  elapsed_secs = now - last_checkpoint_time;
530  if (elapsed_secs >= CheckPointTimeout)
531  continue; /* no sleep for us ... */
532  cur_timeout = CheckPointTimeout - elapsed_secs;
534  {
535  elapsed_secs = now - last_xlog_switch_time;
536  if (elapsed_secs >= XLogArchiveTimeout)
537  continue; /* no sleep for us ... */
538  cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
539  }
540 
541  (void) WaitLatch(MyLatch,
543  cur_timeout * 1000L /* convert to ms */ ,
544  WAIT_EVENT_CHECKPOINTER_MAIN);
545  }
546 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3213
void UnlockBuffers(void)
Definition: bufmgr.c:4768
#define Min(x, y)
Definition: c.h:993
static void UpdateSharedMemoryConfig(void)
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:146
static void ReqCheckpointHandler(SIGNAL_ARGS)
Definition: checkpointer.c:853
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:617
static double ckpt_cached_elapsed
Definition: checkpointer.c:147
static bool ckpt_active
Definition: checkpointer.c:142
static void HandleCheckpointerInterrupts(void)
Definition: checkpointer.c:552
void AbsorbSyncRequests(void)
int CheckPointWarning
Definition: checkpointer.c:136
int CheckPointTimeout
Definition: checkpointer.c:135
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:149
static pg_time_t ckpt_start_time
Definition: checkpointer.c:145
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:1185
void EmitErrorReport(void)
Definition: elog.c:1675
ErrorContextCallback * error_context_stack
Definition: elog.c:95
void FlushErrorState(void)
Definition: elog.c:1831
int errhint(const char *fmt,...)
Definition: elog.c:1322
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:3165
int MyProcPid
Definition: globals.c:45
struct Latch * MyLatch
Definition: globals.c:59
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
void ResetLatch(Latch *latch)
Definition: latch.c:725
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:518
#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:330
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:128
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:152
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void pgstat_before_server_shutdown(int code, Datum arg)
Definition: pgstat.c:465
void pgstat_report_checkpointer(void)
PgStat_CheckpointerStats PendingCheckpointerStats
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:1002
void pg_usleep(long microsec)
Definition: signal.c:53
void smgrdestroyall(void)
Definition: smgr.c:333
void AtEOXact_SMgr(void)
Definition: smgr.c:809
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
PGPROC * MyProc
Definition: proc.c:68
PROC_HDR * ProcGlobal
Definition: proc.c:81
ConditionVariable done_cv
Definition: checkpointer.c:120
ConditionVariable start_cv
Definition: checkpointer.c:119
Latch procLatch
Definition: proc.h:170
Latch * checkpointerLatch
Definition: proc.h:392
PgStat_Counter restartpoints_requested
Definition: pgstat.h:266
PgStat_Counter num_requested
Definition: pgstat.h:264
PgStat_Counter restartpoints_timed
Definition: pgstat.h:265
PgStat_Counter num_timed
Definition: pgstat.h:263
PgStat_Counter restartpoints_performed
Definition: pgstat.h:267
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:7437
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:6359
void CreateCheckPoint(int flags)
Definition: xlog.c:6744
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:146
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:138
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:147
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(), MemoryContextReset(), MemoryContextSwitchTo(), Min, MyLatch, MyProc, MyProcPid, now(), 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(), pqsignal(), ProcGlobal, PGPROC::procLatch, procsignal_sigusr1_handler(), RecoveryInProgress(), ReleaseAuxProcessResources(), ReqCheckpointHandler(), ResetLatch(), PgStat_CheckpointerStats::restartpoints_performed, PgStat_CheckpointerStats::restartpoints_requested, PgStat_CheckpointerStats::restartpoints_timed, RESUME_INTERRUPTS, SIG_DFL, SIG_IGN, 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.

Referenced by AuxiliaryProcessMain().

◆ CheckpointerShmemInit()

void CheckpointerShmemInit ( void  )

Definition at line 892 of file checkpointer.c.

893 {
894  Size size = CheckpointerShmemSize();
895  bool found;
896 
898  ShmemInitStruct("Checkpointer Data",
899  size,
900  &found);
901 
902  if (!found)
903  {
904  /*
905  * First time through, so initialize. Note that we zero the whole
906  * requests array; this is so that CompactCheckpointerRequestQueue can
907  * assume that any pad bytes in the request structs are zeroes.
908  */
909  MemSet(CheckpointerShmem, 0, size);
914  }
915 }
#define MemSet(start, val, len)
Definition: c.h:1009
size_t Size
Definition: c.h:594
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:873
void ConditionVariableInit(ConditionVariable *cv)
int NBuffers
Definition: globals.c:139
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
#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 CreateOrAttachShmemStructs().

◆ CheckpointerShmemSize()

Size CheckpointerShmemSize ( void  )

Definition at line 873 of file checkpointer.c.

874 {
875  Size size;
876 
877  /*
878  * Currently, the size of the requests[] array is arbitrarily set equal to
879  * NBuffers. This may prove too large or small ...
880  */
881  size = offsetof(CheckpointerShmemStruct, requests);
882  size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
883 
884  return size;
885 }
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511

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

Referenced by CalculateShmemSize(), and CheckpointerShmemInit().

◆ CheckpointWriteDelay()

void CheckpointWriteDelay ( int  flags,
double  progress 
)

Definition at line 705 of file checkpointer.c.

706 {
707  static int absorb_counter = WRITES_PER_ABSORB;
708 
709  /* Do nothing if checkpoint is being executed by non-checkpointer process */
710  if (!AmCheckpointerProcess())
711  return;
712 
713  /*
714  * Perform the usual duties and take a nap, unless we're behind schedule,
715  * in which case we just try to catch up as quickly as possible.
716  */
717  if (!(flags & CHECKPOINT_IMMEDIATE) &&
721  {
723  {
724  ConfigReloadPending = false;
726  /* update shmem copies of config variables */
728  }
729 
731  absorb_counter = WRITES_PER_ABSORB;
732 
734 
735  /* Report interim statistics to the cumulative stats system */
737 
738  /*
739  * This sleep used to be connected to bgwriter_delay, typically 200ms.
740  * That resulted in more frequent wakeups if not much work to do.
741  * Checkpointer and bgwriter are no longer related so take the Big
742  * Sleep.
743  */
745  100,
746  WAIT_EVENT_CHECKPOINT_WRITE_DELAY);
748  }
749  else if (--absorb_counter <= 0)
750  {
751  /*
752  * Absorb pending fsync requests after each WRITES_PER_ABSORB write
753  * operations even when we don't sleep, to prevent overflow of the
754  * fsync request queue.
755  */
757  absorb_counter = WRITES_PER_ABSORB;
758  }
759 
760  /* Check for barrier events. */
763 }
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:678
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:774
#define WRITES_PER_ABSORB
Definition: checkpointer.c:130
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:38
@ 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:139

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

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

1321 {
1322  static int ckpt_done = 0;
1323  int new_done;
1324  bool FirstCall = false;
1325 
1327  new_done = CheckpointerShmem->ckpt_done;
1329 
1330  if (new_done != ckpt_done)
1331  FirstCall = true;
1332 
1333  ckpt_done = new_done;
1334 
1335  return FirstCall;
1336 }

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

1088 {
1089  CheckpointerRequest *request;
1090  bool too_full;
1091 
1092  if (!IsUnderPostmaster)
1093  return false; /* probably shouldn't even get here */
1094 
1095  if (AmCheckpointerProcess())
1096  elog(ERROR, "ForwardSyncRequest must not be called in checkpointer");
1097 
1098  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1099 
1100  /*
1101  * If the checkpointer isn't running or the request queue is full, the
1102  * backend will have to perform its own fsync request. But before forcing
1103  * that to happen, we can try to compact the request queue.
1104  */
1105  if (CheckpointerShmem->checkpointer_pid == 0 ||
1108  {
1109  LWLockRelease(CheckpointerCommLock);
1110  return false;
1111  }
1112 
1113  /* OK, insert request */
1115  request->ftag = *ftag;
1116  request->type = type;
1117 
1118  /* If queue is more than half full, nudge the checkpointer to empty it */
1119  too_full = (CheckpointerShmem->num_requests >=
1121 
1122  LWLockRelease(CheckpointerCommLock);
1123 
1124  /* ... but not till after we release the lock */
1125  if (too_full && ProcGlobal->checkpointerLatch)
1127 
1128  return true;
1129 }
static bool CompactCheckpointerRequestQueue(void)
#define ERROR
Definition: elog.h:39
bool IsUnderPostmaster
Definition: globals.c:116
void SetLatch(Latch *latch)
Definition: latch.c:633
const char * type

References AmCheckpointerProcess, CheckpointerShmemStruct::checkpointer_pid, PROC_HDR::checkpointerLatch, CheckpointerShmem, CompactCheckpointerRequestQueue(), elog(), ERROR, CheckpointerRequest::ftag, 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 552 of file checkpointer.c.

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

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

Referenced by CheckpointerMain().

◆ ImmediateCheckpointRequested()

static bool ImmediateCheckpointRequested ( void  )
static

Definition at line 678 of file checkpointer.c.

679 {
681 
682  /*
683  * We don't need to acquire the ckpt_lck in this case because we're only
684  * looking at a single flag bit.
685  */
686  if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
687  return true;
688  return false;
689 }

References CHECKPOINT_IMMEDIATE, CheckpointerShmem, and CheckpointerShmemStruct::ckpt_flags.

Referenced by CheckpointWriteDelay().

◆ IsCheckpointOnSchedule()

static bool IsCheckpointOnSchedule ( double  progress)
static

Definition at line 774 of file checkpointer.c.

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

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

854 {
855  /*
856  * The signaling process should have set ckpt_flags nonzero, so all we
857  * need do is ensure that our main loop gets kicked out of any wait.
858  */
859  SetLatch(MyLatch);
860 }

References MyLatch, and SetLatch().

Referenced by CheckpointerMain().

◆ RequestCheckpoint()

void RequestCheckpoint ( int  flags)

Definition at line 935 of file checkpointer.c.

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

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(), 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 1301 of file checkpointer.c.

1302 {
1303  /* update global shmem state for sync rep */
1305 
1306  /*
1307  * If full_page_writes has been changed by SIGHUP, we update it in shared
1308  * memory and write an XLOG_FPW_CHANGE record.
1309  */
1311 
1312  elog(DEBUG2, "checkpointer updated shared memory configuration values");
1313 }
#define DEBUG2
Definition: elog.h:29
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:923
void UpdateFullPageWrites(void)
Definition: xlog.c:8006

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

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ CheckPointWarning

int CheckPointWarning = 30

Definition at line 136 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ ckpt_active

bool ckpt_active = false
static

Definition at line 142 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_cached_elapsed

double ckpt_cached_elapsed
static

Definition at line 147 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_recptr

XLogRecPtr ckpt_start_recptr
static

Definition at line 146 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ ckpt_start_time

pg_time_t ckpt_start_time
static

Definition at line 145 of file checkpointer.c.

Referenced by CheckpointerMain(), and IsCheckpointOnSchedule().

◆ last_checkpoint_time

pg_time_t last_checkpoint_time
static

Definition at line 149 of file checkpointer.c.

Referenced by CheckpointerMain().

◆ last_xlog_switch_time

pg_time_t last_xlog_switch_time
static

Definition at line 150 of file checkpointer.c.

Referenced by CheckArchiveTimeout(), and CheckpointerMain().