PostgreSQL Source Code  git master
checkpointer.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * checkpointer.c
4  *
5  * The checkpointer is new as of Postgres 9.2. It handles all checkpoints.
6  * Checkpoints are automatically dispatched after a certain amount of time has
7  * elapsed since the last one, and it can be signaled to perform requested
8  * checkpoints as well. (The GUC parameter that mandates a checkpoint every
9  * so many WAL segments is implemented by having backends signal when they
10  * fill WAL segments; the checkpointer itself doesn't watch for the
11  * condition.)
12  *
13  * The checkpointer is started by the postmaster as soon as the startup
14  * subprocess finishes, or as soon as recovery begins if we are doing archive
15  * recovery. It remains alive until the postmaster commands it to terminate.
16  * Normal termination is by SIGUSR2, which instructs the checkpointer to
17  * execute a shutdown checkpoint and then exit(0). (All backends must be
18  * stopped before SIGUSR2 is issued!) Emergency termination is by SIGQUIT;
19  * like any backend, the checkpointer will simply abort and exit on SIGQUIT.
20  *
21  * If the checkpointer exits unexpectedly, the postmaster treats that the same
22  * as a backend crash: shared memory may be corrupted, so remaining backends
23  * should be killed by SIGQUIT and then a recovery cycle started. (Even if
24  * shared memory isn't corrupted, we have lost information about which
25  * files need to be fsync'd for the next checkpoint, and so a system
26  * restart needs to be forced.)
27  *
28  *
29  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
30  *
31  *
32  * IDENTIFICATION
33  * src/backend/postmaster/checkpointer.c
34  *
35  *-------------------------------------------------------------------------
36  */
37 #include "postgres.h"
38 
39 #include <sys/time.h>
40 #include <time.h>
41 
42 #include "access/xlog.h"
43 #include "access/xlog_internal.h"
44 #include "libpq/pqsignal.h"
45 #include "miscadmin.h"
46 #include "pgstat.h"
47 #include "postmaster/bgwriter.h"
48 #include "postmaster/interrupt.h"
49 #include "replication/syncrep.h"
50 #include "storage/bufmgr.h"
52 #include "storage/fd.h"
53 #include "storage/ipc.h"
54 #include "storage/lwlock.h"
55 #include "storage/proc.h"
56 #include "storage/procsignal.h"
57 #include "storage/shmem.h"
58 #include "storage/smgr.h"
59 #include "storage/spin.h"
60 #include "utils/guc.h"
61 #include "utils/memutils.h"
62 #include "utils/resowner.h"
63 
64 
65 /*----------
66  * Shared memory area for communication between checkpointer and backends
67  *
68  * The ckpt counters allow backends to watch for completion of a checkpoint
69  * request they send. Here's how it works:
70  * * At start of a checkpoint, checkpointer reads (and clears) the request
71  * flags and increments ckpt_started, while holding ckpt_lck.
72  * * On completion of a checkpoint, checkpointer sets ckpt_done to
73  * equal ckpt_started.
74  * * On failure of a checkpoint, checkpointer increments ckpt_failed
75  * and sets ckpt_done to equal ckpt_started.
76  *
77  * The algorithm for backends is:
78  * 1. Record current values of ckpt_failed and ckpt_started, and
79  * set request flags, while holding ckpt_lck.
80  * 2. Send signal to request checkpoint.
81  * 3. Sleep until ckpt_started changes. Now you know a checkpoint has
82  * begun since you started this algorithm (although *not* that it was
83  * specifically initiated by your signal), and that it is using your flags.
84  * 4. Record new value of ckpt_started.
85  * 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
86  * arithmetic here in case counters wrap around.) Now you know a
87  * checkpoint has started and completed, but not whether it was
88  * successful.
89  * 6. If ckpt_failed is different from the originally saved value,
90  * assume request failed; otherwise it was definitely successful.
91  *
92  * ckpt_flags holds the OR of the checkpoint request flags sent by all
93  * requesting backends since the last checkpoint start. The flags are
94  * chosen so that OR'ing is the correct way to combine multiple requests.
95  *
96  * num_backend_writes is used to count the number of buffer writes performed
97  * by user backend processes. This counter should be wide enough that it
98  * can't overflow during a single processing cycle. num_backend_fsync
99  * counts the subset of those writes that also had to do their own fsync,
100  * because the checkpointer failed to absorb their request.
101  *
102  * The requests array holds fsync requests sent by backends and not yet
103  * absorbed by the checkpointer.
104  *
105  * Unlike the checkpoint fields, num_backend_writes, num_backend_fsync, and
106  * the requests fields are protected by CheckpointerCommLock.
107  *----------
108  */
109 typedef struct
110 {
111  SyncRequestType type; /* request type */
112  FileTag ftag; /* file identifier */
114 
115 typedef struct
116 {
117  pid_t checkpointer_pid; /* PID (0 if not started) */
118 
119  slock_t ckpt_lck; /* protects all the ckpt_* fields */
120 
121  int ckpt_started; /* advances when checkpoint starts */
122  int ckpt_done; /* advances when checkpoint done */
123  int ckpt_failed; /* advances when checkpoint fails */
124 
125  int ckpt_flags; /* checkpoint flags, as defined in xlog.h */
126 
127  ConditionVariable start_cv; /* signaled when ckpt_started advances */
128  ConditionVariable done_cv; /* signaled when ckpt_done advances */
129 
130  uint32 num_backend_writes; /* counts user backend buffer writes */
131  uint32 num_backend_fsync; /* counts user backend fsync calls */
132 
133  int num_requests; /* current # of requests */
134  int max_requests; /* allocated array size */
135  CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER];
137 
139 
140 /* interval for calling AbsorbSyncRequests in CheckpointWriteDelay */
141 #define WRITES_PER_ABSORB 1000
142 
143 /*
144  * GUC parameters
145  */
149 
150 /*
151  * Private state
152  */
153 static bool ckpt_active = false;
154 
155 /* these values are valid when ckpt_active is true: */
158 static double ckpt_cached_elapsed;
159 
162 
163 /* Prototypes for private functions */
164 
165 static void HandleCheckpointerInterrupts(void);
166 static void CheckArchiveTimeout(void);
167 static bool IsCheckpointOnSchedule(double progress);
168 static bool ImmediateCheckpointRequested(void);
169 static bool CompactCheckpointerRequestQueue(void);
170 static void UpdateSharedMemoryConfig(void);
171 
172 /* Signal handlers */
173 static void ReqCheckpointHandler(SIGNAL_ARGS);
174 
175 
176 /*
177  * Main entry point for checkpointer process
178  *
179  * This is invoked from AuxiliaryProcessMain, which has already created the
180  * basic execution environment, but not enabled signals yet.
181  */
182 void
184 {
185  sigjmp_buf local_sigjmp_buf;
186  MemoryContext checkpointer_context;
187 
188  CheckpointerShmem->checkpointer_pid = MyProcPid;
189 
190  /*
191  * Properly accept or ignore signals the postmaster might send us
192  *
193  * Note: we deliberately ignore SIGTERM, because during a standard Unix
194  * system shutdown cycle, init will SIGTERM all processes at once. We
195  * want to wait for the backends to exit, whereupon the postmaster will
196  * tell us it's okay to shut down (via SIGUSR2).
197  */
199  pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
200  pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
206 
207  /*
208  * Reset some signals that are accepted by postmaster but not here
209  */
211 
212  /* We allow SIGQUIT (quickdie) at all times */
213  sigdelset(&BlockSig, SIGQUIT);
214 
215  /*
216  * Initialize so that first time-driven event happens at the correct time.
217  */
219 
220  /*
221  * Create a memory context that we will do all our work in. We do this so
222  * that we can reset the context during error recovery and thereby avoid
223  * possible memory leaks. Formerly this code just ran in
224  * TopMemoryContext, but resetting that would be a really bad idea.
225  */
226  checkpointer_context = AllocSetContextCreate(TopMemoryContext,
227  "Checkpointer",
229  MemoryContextSwitchTo(checkpointer_context);
230 
231  /*
232  * If an exception is encountered, processing resumes here.
233  *
234  * See notes in postgres.c about the design of this coding.
235  */
236  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
237  {
238  /* Since not using PG_TRY, must reset error stack by hand */
239  error_context_stack = NULL;
240 
241  /* Prevent interrupts while cleaning up */
242  HOLD_INTERRUPTS();
243 
244  /* Report the error to the server log */
245  EmitErrorReport();
246 
247  /*
248  * These operations are really just a minimal subset of
249  * AbortTransaction(). We don't have very many resources to worry
250  * about in checkpointer, but we do have LWLocks, buffers, and temp
251  * files.
252  */
256  AbortBufferIO();
257  UnlockBuffers();
259  AtEOXact_Buffers(false);
260  AtEOXact_SMgr();
261  AtEOXact_Files(false);
262  AtEOXact_HashTables(false);
263 
264  /* Warn any waiting backends that the checkpoint failed. */
265  if (ckpt_active)
266  {
267  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
268  CheckpointerShmem->ckpt_failed++;
269  CheckpointerShmem->ckpt_done = CheckpointerShmem->ckpt_started;
270  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
271 
272  ConditionVariableBroadcast(&CheckpointerShmem->done_cv);
273 
274  ckpt_active = false;
275  }
276 
277  /*
278  * Now return to normal top-level context and clear ErrorContext for
279  * next time.
280  */
281  MemoryContextSwitchTo(checkpointer_context);
282  FlushErrorState();
283 
284  /* Flush any leaked data in the top-level context */
285  MemoryContextResetAndDeleteChildren(checkpointer_context);
286 
287  /* Now we can allow interrupts again */
289 
290  /*
291  * Sleep at least 1 second after any error. A write error is likely
292  * to be repeated, and we don't want to be filling the error logs as
293  * fast as we can.
294  */
295  pg_usleep(1000000L);
296 
297  /*
298  * Close all open files after any error. This is helpful on Windows,
299  * where holding deleted files open causes various strange errors.
300  * It's not clear we need it elsewhere, but shouldn't hurt.
301  */
302  smgrcloseall();
303  }
304 
305  /* We can now handle ereport(ERROR) */
306  PG_exception_stack = &local_sigjmp_buf;
307 
308  /*
309  * Unblock signals (they were blocked when the postmaster forked us)
310  */
312 
313  /*
314  * Ensure all shared memory values are set correctly for the config. Doing
315  * this here ensures no race conditions from other concurrent updaters.
316  */
318 
319  /*
320  * Advertise our latch that backends can use to wake us up while we're
321  * sleeping.
322  */
324 
325  /*
326  * Loop forever
327  */
328  for (;;)
329  {
330  bool do_checkpoint = false;
331  int flags = 0;
332  pg_time_t now;
333  int elapsed_secs;
334  int cur_timeout;
335 
336  /* Clear any already-pending wakeups */
338 
339  /*
340  * Process any requests or signals received recently.
341  */
344 
345  /*
346  * Detect a pending checkpoint request by checking whether the flags
347  * word in shared memory is nonzero. We shouldn't need to acquire the
348  * ckpt_lck for this.
349  */
350  if (((volatile CheckpointerShmemStruct *) CheckpointerShmem)->ckpt_flags)
351  {
352  do_checkpoint = true;
354  }
355 
356  /*
357  * Force a checkpoint if too much time has elapsed since the last one.
358  * Note that we count a timed checkpoint in stats only when this
359  * occurs without an external request, but we set the CAUSE_TIME flag
360  * bit even if there is also an external request.
361  */
362  now = (pg_time_t) time(NULL);
363  elapsed_secs = now - last_checkpoint_time;
364  if (elapsed_secs >= CheckPointTimeout)
365  {
366  if (!do_checkpoint)
368  do_checkpoint = true;
369  flags |= CHECKPOINT_CAUSE_TIME;
370  }
371 
372  /*
373  * Do a checkpoint if requested.
374  */
375  if (do_checkpoint)
376  {
377  bool ckpt_performed = false;
378  bool do_restartpoint;
379 
380  /*
381  * Check if we should perform a checkpoint or a restartpoint. As a
382  * side-effect, RecoveryInProgress() initializes TimeLineID if
383  * it's not set yet.
384  */
385  do_restartpoint = RecoveryInProgress();
386 
387  /*
388  * Atomically fetch the request flags to figure out what kind of a
389  * checkpoint we should perform, and increase the started-counter
390  * to acknowledge that we've started a new checkpoint.
391  */
392  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
393  flags |= CheckpointerShmem->ckpt_flags;
394  CheckpointerShmem->ckpt_flags = 0;
395  CheckpointerShmem->ckpt_started++;
396  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
397 
398  ConditionVariableBroadcast(&CheckpointerShmem->start_cv);
399 
400  /*
401  * The end-of-recovery checkpoint is a real checkpoint that's
402  * performed while we're still in recovery.
403  */
404  if (flags & CHECKPOINT_END_OF_RECOVERY)
405  do_restartpoint = false;
406 
407  /*
408  * We will warn if (a) too soon since last checkpoint (whatever
409  * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
410  * since the last checkpoint start. Note in particular that this
411  * implementation will not generate warnings caused by
412  * CheckPointTimeout < CheckPointWarning.
413  */
414  if (!do_restartpoint &&
415  (flags & CHECKPOINT_CAUSE_XLOG) &&
416  elapsed_secs < CheckPointWarning)
417  ereport(LOG,
418  (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
419  "checkpoints are occurring too frequently (%d seconds apart)",
420  elapsed_secs,
421  elapsed_secs),
422  errhint("Consider increasing the configuration parameter \"max_wal_size\".")));
423 
424  /*
425  * Initialize checkpointer-private variables used during
426  * checkpoint.
427  */
428  ckpt_active = true;
429  if (do_restartpoint)
431  else
435 
436  /*
437  * Do the checkpoint.
438  */
439  if (!do_restartpoint)
440  {
441  CreateCheckPoint(flags);
442  ckpt_performed = true;
443  }
444  else
445  ckpt_performed = CreateRestartPoint(flags);
446 
447  /*
448  * After any checkpoint, close all smgr files. This is so we
449  * won't hang onto smgr references to deleted files indefinitely.
450  */
451  smgrcloseall();
452 
453  /*
454  * Indicate checkpoint completion to any waiting backends.
455  */
456  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
457  CheckpointerShmem->ckpt_done = CheckpointerShmem->ckpt_started;
458  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
459 
460  ConditionVariableBroadcast(&CheckpointerShmem->done_cv);
461 
462  if (ckpt_performed)
463  {
464  /*
465  * Note we record the checkpoint start time not end time as
466  * last_checkpoint_time. This is so that time-driven
467  * checkpoints happen at a predictable spacing.
468  */
469  last_checkpoint_time = now;
470  }
471  else
472  {
473  /*
474  * We were not able to perform the restartpoint (checkpoints
475  * throw an ERROR in case of error). Most likely because we
476  * have not received any new checkpoint WAL records since the
477  * last restartpoint. Try again in 15 s.
478  */
479  last_checkpoint_time = now - CheckPointTimeout + 15;
480  }
481 
482  ckpt_active = false;
483  }
484 
485  /* Check for archive_timeout and switch xlog files if necessary. */
487 
488  /*
489  * Send off activity statistics to the stats collector. (The reason
490  * why we re-use bgwriter-related code for this is that the bgwriter
491  * and checkpointer used to be just one process. It's probably not
492  * worth the trouble to split the stats support into two independent
493  * stats message types.)
494  */
496 
497  /*
498  * Sleep until we are signaled or it's time for another checkpoint or
499  * xlog file switch.
500  */
501  now = (pg_time_t) time(NULL);
502  elapsed_secs = now - last_checkpoint_time;
503  if (elapsed_secs >= CheckPointTimeout)
504  continue; /* no sleep for us ... */
505  cur_timeout = CheckPointTimeout - elapsed_secs;
507  {
508  elapsed_secs = now - last_xlog_switch_time;
509  if (elapsed_secs >= XLogArchiveTimeout)
510  continue; /* no sleep for us ... */
511  cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
512  }
513 
514  (void) WaitLatch(MyLatch,
516  cur_timeout * 1000L /* convert to ms */ ,
518  }
519 }
520 
521 /*
522  * Process any new interrupts.
523  */
524 static void
526 {
529 
531  {
532  ConfigReloadPending = false;
534 
535  /*
536  * Checkpointer is the last process to shut down, so we ask it to
537  * hold the keys for a range of other tasks required most of which
538  * have nothing to do with checkpointing at all.
539  *
540  * For various reasons, some config values can change dynamically
541  * so the primary copy of them is held in shared memory to make
542  * sure all backends see the same value. We make Checkpointer
543  * responsible for updating the shared memory copy if the
544  * parameter setting changes because of SIGHUP.
545  */
547  }
549  {
550  /*
551  * From here on, elog(ERROR) should end with exit(1), not send
552  * control back to the sigsetjmp block above
553  */
554  ExitOnAnyError = true;
555  /* Close down the database */
556  ShutdownXLOG(0, 0);
557  /* Normal exit from the checkpointer is here */
558  proc_exit(0); /* done */
559  }
560 }
561 
562 /*
563  * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
564  *
565  * This will switch to a new WAL file and force an archive file write if
566  * meaningful activity is recorded in the current WAL file. This includes most
567  * writes, including just a single checkpoint record, but excludes WAL records
568  * that were inserted with the XLOG_MARK_UNIMPORTANT flag being set (like
569  * snapshots of running transactions). Such records, depending on
570  * configuration, occur on regular intervals and don't contain important
571  * information. This avoids generating archives with a few unimportant
572  * records.
573  */
574 static void
576 {
577  pg_time_t now;
578  pg_time_t last_time;
579  XLogRecPtr last_switch_lsn;
580 
582  return;
583 
584  now = (pg_time_t) time(NULL);
585 
586  /* First we do a quick check using possibly-stale local state. */
587  if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
588  return;
589 
590  /*
591  * Update local state ... note that last_xlog_switch_time is the last time
592  * a switch was performed *or requested*.
593  */
594  last_time = GetLastSegSwitchData(&last_switch_lsn);
595 
597 
598  /* Now we can do the real checks */
599  if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
600  {
601  /*
602  * Switch segment only when "important" WAL has been logged since the
603  * last segment switch (last_switch_lsn points to end of segment
604  * switch occurred in).
605  */
606  if (GetLastImportantRecPtr() > last_switch_lsn)
607  {
608  XLogRecPtr switchpoint;
609 
610  /* mark switch as unimportant, avoids triggering checkpoints */
611  switchpoint = RequestXLogSwitch(true);
612 
613  /*
614  * If the returned pointer points exactly to a segment boundary,
615  * assume nothing happened.
616  */
617  if (XLogSegmentOffset(switchpoint, wal_segment_size) != 0)
618  elog(DEBUG1, "write-ahead log switch forced (archive_timeout=%d)",
620  }
621 
622  /*
623  * Update state in any case, so we don't retry constantly when the
624  * system is idle.
625  */
627  }
628 }
629 
630 /*
631  * Returns true if an immediate checkpoint request is pending. (Note that
632  * this does not check the *current* checkpoint's IMMEDIATE flag, but whether
633  * there is one pending behind it.)
634  */
635 static bool
637 {
639 
640  /*
641  * We don't need to acquire the ckpt_lck in this case because we're only
642  * looking at a single flag bit.
643  */
644  if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
645  return true;
646  return false;
647 }
648 
649 /*
650  * CheckpointWriteDelay -- control rate of checkpoint
651  *
652  * This function is called after each page write performed by BufferSync().
653  * It is responsible for throttling BufferSync()'s write rate to hit
654  * checkpoint_completion_target.
655  *
656  * The checkpoint request flags should be passed in; currently the only one
657  * examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
658  *
659  * 'progress' is an estimate of how much of the work has been done, as a
660  * fraction between 0.0 meaning none, and 1.0 meaning all done.
661  */
662 void
663 CheckpointWriteDelay(int flags, double progress)
664 {
665  static int absorb_counter = WRITES_PER_ABSORB;
666 
667  /* Do nothing if checkpoint is being executed by non-checkpointer process */
668  if (!AmCheckpointerProcess())
669  return;
670 
671  /*
672  * Perform the usual duties and take a nap, unless we're behind schedule,
673  * in which case we just try to catch up as quickly as possible.
674  */
675  if (!(flags & CHECKPOINT_IMMEDIATE) &&
678  IsCheckpointOnSchedule(progress))
679  {
681  {
682  ConfigReloadPending = false;
684  /* update shmem copies of config variables */
686  }
687 
689  absorb_counter = WRITES_PER_ABSORB;
690 
692 
693  /*
694  * Report interim activity statistics to the stats collector.
695  */
697 
698  /*
699  * This sleep used to be connected to bgwriter_delay, typically 200ms.
700  * That resulted in more frequent wakeups if not much work to do.
701  * Checkpointer and bgwriter are no longer related so take the Big
702  * Sleep.
703  */
704  pg_usleep(100000L);
705  }
706  else if (--absorb_counter <= 0)
707  {
708  /*
709  * Absorb pending fsync requests after each WRITES_PER_ABSORB write
710  * operations even when we don't sleep, to prevent overflow of the
711  * fsync request queue.
712  */
714  absorb_counter = WRITES_PER_ABSORB;
715  }
716 
717  /* Check for barrier events. */
720 }
721 
722 /*
723  * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
724  * (or restartpoint) in time?
725  *
726  * Compares the current progress against the time/segments elapsed since last
727  * checkpoint, and returns true if the progress we've made this far is greater
728  * than the elapsed time/segments.
729  */
730 static bool
732 {
733  XLogRecPtr recptr;
734  struct timeval now;
735  double elapsed_xlogs,
736  elapsed_time;
737 
739 
740  /* Scale progress according to checkpoint_completion_target. */
741  progress *= CheckPointCompletionTarget;
742 
743  /*
744  * Check against the cached value first. Only do the more expensive
745  * calculations once we reach the target previously calculated. Since
746  * neither time or WAL insert pointer moves backwards, a freshly
747  * calculated value can only be greater than or equal to the cached value.
748  */
749  if (progress < ckpt_cached_elapsed)
750  return false;
751 
752  /*
753  * Check progress against WAL segments written and CheckPointSegments.
754  *
755  * We compare the current WAL insert location against the location
756  * computed before calling CreateCheckPoint. The code in XLogInsert that
757  * actually triggers a checkpoint when CheckPointSegments is exceeded
758  * compares against RedoRecPtr, so this is not completely accurate.
759  * However, it's good enough for our purposes, we're only calculating an
760  * estimate anyway.
761  *
762  * During recovery, we compare last replayed WAL record's location with
763  * the location computed before calling CreateRestartPoint. That maintains
764  * the same pacing as we have during checkpoints in normal operation, but
765  * we might exceed max_wal_size by a fair amount. That's because there can
766  * be a large gap between a checkpoint's redo-pointer and the checkpoint
767  * record itself, and we only start the restartpoint after we've seen the
768  * checkpoint record. (The gap is typically up to CheckPointSegments *
769  * checkpoint_completion_target where checkpoint_completion_target is the
770  * value that was in effect when the WAL was generated).
771  */
772  if (RecoveryInProgress())
773  recptr = GetXLogReplayRecPtr(NULL);
774  else
775  recptr = GetInsertRecPtr();
776  elapsed_xlogs = (((double) (recptr - ckpt_start_recptr)) /
778 
779  if (progress < elapsed_xlogs)
780  {
781  ckpt_cached_elapsed = elapsed_xlogs;
782  return false;
783  }
784 
785  /*
786  * Check progress against time elapsed and checkpoint_timeout.
787  */
788  gettimeofday(&now, NULL);
789  elapsed_time = ((double) ((pg_time_t) now.tv_sec - ckpt_start_time) +
790  now.tv_usec / 1000000.0) / CheckPointTimeout;
791 
792  if (progress < elapsed_time)
793  {
795  return false;
796  }
797 
798  /* It looks like we're on schedule. */
799  return true;
800 }
801 
802 
803 /* --------------------------------
804  * signal handler routines
805  * --------------------------------
806  */
807 
808 /* SIGINT: set flag to run a normal checkpoint right away */
809 static void
811 {
812  int save_errno = errno;
813 
814  /*
815  * The signalling process should have set ckpt_flags nonzero, so all we
816  * need do is ensure that our main loop gets kicked out of any wait.
817  */
818  SetLatch(MyLatch);
819 
820  errno = save_errno;
821 }
822 
823 
824 /* --------------------------------
825  * communication with backends
826  * --------------------------------
827  */
828 
829 /*
830  * CheckpointerShmemSize
831  * Compute space needed for checkpointer-related shared memory
832  */
833 Size
835 {
836  Size size;
837 
838  /*
839  * Currently, the size of the requests[] array is arbitrarily set equal to
840  * NBuffers. This may prove too large or small ...
841  */
842  size = offsetof(CheckpointerShmemStruct, requests);
843  size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
844 
845  return size;
846 }
847 
848 /*
849  * CheckpointerShmemInit
850  * Allocate and initialize checkpointer-related shared memory
851  */
852 void
854 {
855  Size size = CheckpointerShmemSize();
856  bool found;
857 
858  CheckpointerShmem = (CheckpointerShmemStruct *)
859  ShmemInitStruct("Checkpointer Data",
860  size,
861  &found);
862 
863  if (!found)
864  {
865  /*
866  * First time through, so initialize. Note that we zero the whole
867  * requests array; this is so that CompactCheckpointerRequestQueue can
868  * assume that any pad bytes in the request structs are zeroes.
869  */
870  MemSet(CheckpointerShmem, 0, size);
871  SpinLockInit(&CheckpointerShmem->ckpt_lck);
872  CheckpointerShmem->max_requests = NBuffers;
873  ConditionVariableInit(&CheckpointerShmem->start_cv);
874  ConditionVariableInit(&CheckpointerShmem->done_cv);
875  }
876 }
877 
878 /*
879  * RequestCheckpoint
880  * Called in backend processes to request a checkpoint
881  *
882  * flags is a bitwise OR of the following:
883  * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
884  * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
885  * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
886  * ignoring checkpoint_completion_target parameter.
887  * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred
888  * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
889  * CHECKPOINT_END_OF_RECOVERY).
890  * CHECKPOINT_WAIT: wait for completion before returning (otherwise,
891  * just signal checkpointer to do it, and return).
892  * CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
893  * (This affects logging, and in particular enables CheckPointWarning.)
894  */
895 void
897 {
898  int ntries;
899  int old_failed,
900  old_started;
901 
902  /*
903  * If in a standalone backend, just do it ourselves.
904  */
906  {
907  /*
908  * There's no point in doing slow checkpoints in a standalone backend,
909  * because there's no other backends the checkpoint could disrupt.
910  */
912 
913  /*
914  * After any checkpoint, close all smgr files. This is so we won't
915  * hang onto smgr references to deleted files indefinitely.
916  */
917  smgrcloseall();
918 
919  return;
920  }
921 
922  /*
923  * Atomically set the request flags, and take a snapshot of the counters.
924  * When we see ckpt_started > old_started, we know the flags we set here
925  * have been seen by checkpointer.
926  *
927  * Note that we OR the flags with any existing flags, to avoid overriding
928  * a "stronger" request by another backend. The flag senses must be
929  * chosen to make this work!
930  */
931  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
932 
933  old_failed = CheckpointerShmem->ckpt_failed;
934  old_started = CheckpointerShmem->ckpt_started;
935  CheckpointerShmem->ckpt_flags |= (flags | CHECKPOINT_REQUESTED);
936 
937  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
938 
939  /*
940  * Send signal to request checkpoint. It's possible that the checkpointer
941  * hasn't started yet, or is in process of restarting, so we will retry a
942  * few times if needed. (Actually, more than a few times, since on slow
943  * or overloaded buildfarm machines, it's been observed that the
944  * checkpointer can take several seconds to start.) However, if not told
945  * to wait for the checkpoint to occur, we consider failure to send the
946  * signal to be nonfatal and merely LOG it. The checkpointer should see
947  * the request when it does start, with or without getting a signal.
948  */
949 #define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
950  for (ntries = 0;; ntries++)
951  {
952  if (CheckpointerShmem->checkpointer_pid == 0)
953  {
954  if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
955  {
956  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
957  "could not signal for checkpoint: checkpointer is not running");
958  break;
959  }
960  }
961  else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
962  {
963  if (ntries >= MAX_SIGNAL_TRIES || !(flags & CHECKPOINT_WAIT))
964  {
965  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
966  "could not signal for checkpoint: %m");
967  break;
968  }
969  }
970  else
971  break; /* signal sent successfully */
972 
974  pg_usleep(100000L); /* wait 0.1 sec, then retry */
975  }
976 
977  /*
978  * If requested, wait for completion. We detect completion according to
979  * the algorithm given above.
980  */
981  if (flags & CHECKPOINT_WAIT)
982  {
983  int new_started,
984  new_failed;
985 
986  /* Wait for a new checkpoint to start. */
987  ConditionVariablePrepareToSleep(&CheckpointerShmem->start_cv);
988  for (;;)
989  {
990  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
991  new_started = CheckpointerShmem->ckpt_started;
992  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
993 
994  if (new_started != old_started)
995  break;
996 
997  ConditionVariableSleep(&CheckpointerShmem->start_cv,
999  }
1001 
1002  /*
1003  * We are waiting for ckpt_done >= new_started, in a modulo sense.
1004  */
1005  ConditionVariablePrepareToSleep(&CheckpointerShmem->done_cv);
1006  for (;;)
1007  {
1008  int new_done;
1009 
1010  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1011  new_done = CheckpointerShmem->ckpt_done;
1012  new_failed = CheckpointerShmem->ckpt_failed;
1013  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1014 
1015  if (new_done - new_started >= 0)
1016  break;
1017 
1018  ConditionVariableSleep(&CheckpointerShmem->done_cv,
1020  }
1022 
1023  if (new_failed != old_failed)
1024  ereport(ERROR,
1025  (errmsg("checkpoint request failed"),
1026  errhint("Consult recent messages in the server log for details.")));
1027  }
1028 }
1029 
1030 /*
1031  * ForwardSyncRequest
1032  * Forward a file-fsync request from a backend to the checkpointer
1033  *
1034  * Whenever a backend is compelled to write directly to a relation
1035  * (which should be seldom, if the background writer is getting its job done),
1036  * the backend calls this routine to pass over knowledge that the relation
1037  * is dirty and must be fsync'd before next checkpoint. We also use this
1038  * opportunity to count such writes for statistical purposes.
1039  *
1040  * To avoid holding the lock for longer than necessary, we normally write
1041  * to the requests[] queue without checking for duplicates. The checkpointer
1042  * will have to eliminate dups internally anyway. However, if we discover
1043  * that the queue is full, we make a pass over the entire queue to compact
1044  * it. This is somewhat expensive, but the alternative is for the backend
1045  * to perform its own fsync, which is far more expensive in practice. It
1046  * is theoretically possible a backend fsync might still be necessary, if
1047  * the queue is full and contains no duplicate entries. In that case, we
1048  * let the backend know by returning false.
1049  */
1050 bool
1052 {
1053  CheckpointerRequest *request;
1054  bool too_full;
1055 
1056  if (!IsUnderPostmaster)
1057  return false; /* probably shouldn't even get here */
1058 
1059  if (AmCheckpointerProcess())
1060  elog(ERROR, "ForwardSyncRequest must not be called in checkpointer");
1061 
1062  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1063 
1064  /* Count all backend writes regardless of if they fit in the queue */
1066  CheckpointerShmem->num_backend_writes++;
1067 
1068  /*
1069  * If the checkpointer isn't running or the request queue is full, the
1070  * backend will have to perform its own fsync request. But before forcing
1071  * that to happen, we can try to compact the request queue.
1072  */
1073  if (CheckpointerShmem->checkpointer_pid == 0 ||
1074  (CheckpointerShmem->num_requests >= CheckpointerShmem->max_requests &&
1076  {
1077  /*
1078  * Count the subset of writes where backends have to do their own
1079  * fsync
1080  */
1082  CheckpointerShmem->num_backend_fsync++;
1083  LWLockRelease(CheckpointerCommLock);
1084  return false;
1085  }
1086 
1087  /* OK, insert request */
1088  request = &CheckpointerShmem->requests[CheckpointerShmem->num_requests++];
1089  request->ftag = *ftag;
1090  request->type = type;
1091 
1092  /* If queue is more than half full, nudge the checkpointer to empty it */
1093  too_full = (CheckpointerShmem->num_requests >=
1094  CheckpointerShmem->max_requests / 2);
1095 
1096  LWLockRelease(CheckpointerCommLock);
1097 
1098  /* ... but not till after we release the lock */
1099  if (too_full && ProcGlobal->checkpointerLatch)
1101 
1102  return true;
1103 }
1104 
1105 /*
1106  * CompactCheckpointerRequestQueue
1107  * Remove duplicates from the request queue to avoid backend fsyncs.
1108  * Returns "true" if any entries were removed.
1109  *
1110  * Although a full fsync request queue is not common, it can lead to severe
1111  * performance problems when it does happen. So far, this situation has
1112  * only been observed to occur when the system is under heavy write load,
1113  * and especially during the "sync" phase of a checkpoint. Without this
1114  * logic, each backend begins doing an fsync for every block written, which
1115  * gets very expensive and can slow down the whole system.
1116  *
1117  * Trying to do this every time the queue is full could lose if there
1118  * aren't any removable entries. But that should be vanishingly rare in
1119  * practice: there's one queue entry per shared buffer.
1120  */
1121 static bool
1123 {
1124  struct CheckpointerSlotMapping
1125  {
1126  CheckpointerRequest request;
1127  int slot;
1128  };
1129 
1130  int n,
1131  preserve_count;
1132  int num_skipped = 0;
1133  HASHCTL ctl;
1134  HTAB *htab;
1135  bool *skip_slot;
1136 
1137  /* must hold CheckpointerCommLock in exclusive mode */
1138  Assert(LWLockHeldByMe(CheckpointerCommLock));
1139 
1140  /* Initialize skip_slot array */
1141  skip_slot = palloc0(sizeof(bool) * CheckpointerShmem->num_requests);
1142 
1143  /* Initialize temporary hash table */
1144  MemSet(&ctl, 0, sizeof(ctl));
1145  ctl.keysize = sizeof(CheckpointerRequest);
1146  ctl.entrysize = sizeof(struct CheckpointerSlotMapping);
1147  ctl.hcxt = CurrentMemoryContext;
1148 
1149  htab = hash_create("CompactCheckpointerRequestQueue",
1150  CheckpointerShmem->num_requests,
1151  &ctl,
1153 
1154  /*
1155  * The basic idea here is that a request can be skipped if it's followed
1156  * by a later, identical request. It might seem more sensible to work
1157  * backwards from the end of the queue and check whether a request is
1158  * *preceded* by an earlier, identical request, in the hopes of doing less
1159  * copying. But that might change the semantics, if there's an
1160  * intervening SYNC_FORGET_REQUEST or SYNC_FILTER_REQUEST, so we do it
1161  * this way. It would be possible to be even smarter if we made the code
1162  * below understand the specific semantics of such requests (it could blow
1163  * away preceding entries that would end up being canceled anyhow), but
1164  * it's not clear that the extra complexity would buy us anything.
1165  */
1166  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1167  {
1168  CheckpointerRequest *request;
1169  struct CheckpointerSlotMapping *slotmap;
1170  bool found;
1171 
1172  /*
1173  * We use the request struct directly as a hashtable key. This
1174  * assumes that any padding bytes in the structs are consistently the
1175  * same, which should be okay because we zeroed them in
1176  * CheckpointerShmemInit. Note also that RelFileNode had better
1177  * contain no pad bytes.
1178  */
1179  request = &CheckpointerShmem->requests[n];
1180  slotmap = hash_search(htab, request, HASH_ENTER, &found);
1181  if (found)
1182  {
1183  /* Duplicate, so mark the previous occurrence as skippable */
1184  skip_slot[slotmap->slot] = true;
1185  num_skipped++;
1186  }
1187  /* Remember slot containing latest occurrence of this request value */
1188  slotmap->slot = n;
1189  }
1190 
1191  /* Done with the hash table. */
1192  hash_destroy(htab);
1193 
1194  /* If no duplicates, we're out of luck. */
1195  if (!num_skipped)
1196  {
1197  pfree(skip_slot);
1198  return false;
1199  }
1200 
1201  /* We found some duplicates; remove them. */
1202  preserve_count = 0;
1203  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1204  {
1205  if (skip_slot[n])
1206  continue;
1207  CheckpointerShmem->requests[preserve_count++] = CheckpointerShmem->requests[n];
1208  }
1209  ereport(DEBUG1,
1210  (errmsg("compacted fsync request queue from %d entries to %d entries",
1211  CheckpointerShmem->num_requests, preserve_count)));
1212  CheckpointerShmem->num_requests = preserve_count;
1213 
1214  /* Cleanup. */
1215  pfree(skip_slot);
1216  return true;
1217 }
1218 
1219 /*
1220  * AbsorbSyncRequests
1221  * Retrieve queued sync requests and pass them to sync mechanism.
1222  *
1223  * This is exported because it must be called during CreateCheckPoint;
1224  * we have to be sure we have accepted all pending requests just before
1225  * we start fsync'ing. Since CreateCheckPoint sometimes runs in
1226  * non-checkpointer processes, do nothing if not checkpointer.
1227  */
1228 void
1230 {
1231  CheckpointerRequest *requests = NULL;
1232  CheckpointerRequest *request;
1233  int n;
1234 
1235  if (!AmCheckpointerProcess())
1236  return;
1237 
1238  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1239 
1240  /* Transfer stats counts into pending pgstats message */
1242  BgWriterStats.m_buf_fsync_backend += CheckpointerShmem->num_backend_fsync;
1243 
1244  CheckpointerShmem->num_backend_writes = 0;
1245  CheckpointerShmem->num_backend_fsync = 0;
1246 
1247  /*
1248  * We try to avoid holding the lock for a long time by copying the request
1249  * array, and processing the requests after releasing the lock.
1250  *
1251  * Once we have cleared the requests from shared memory, we have to PANIC
1252  * if we then fail to absorb them (eg, because our hashtable runs out of
1253  * memory). This is because the system cannot run safely if we are unable
1254  * to fsync what we have been told to fsync. Fortunately, the hashtable
1255  * is so small that the problem is quite unlikely to arise in practice.
1256  */
1257  n = CheckpointerShmem->num_requests;
1258  if (n > 0)
1259  {
1260  requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1261  memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1262  }
1263 
1265 
1266  CheckpointerShmem->num_requests = 0;
1267 
1268  LWLockRelease(CheckpointerCommLock);
1269 
1270  for (request = requests; n > 0; request++, n--)
1271  RememberSyncRequest(&request->ftag, request->type);
1272 
1273  END_CRIT_SECTION();
1274 
1275  if (requests)
1276  pfree(requests);
1277 }
1278 
1279 /*
1280  * Update any shared memory configurations based on config parameters
1281  */
1282 static void
1284 {
1285  /* update global shmem state for sync rep */
1287 
1288  /*
1289  * If full_page_writes has been changed by SIGHUP, we update it in shared
1290  * memory and write an XLOG_FPW_CHANGE record.
1291  */
1293 
1294  elog(DEBUG2, "checkpointer updated shared memory configuration values");
1295 }
1296 
1297 /*
1298  * FirstCallSinceLastCheckpoint allows a process to take an action once
1299  * per checkpoint cycle by asynchronously checking for checkpoint completion.
1300  */
1301 bool
1303 {
1304  static int ckpt_done = 0;
1305  int new_done;
1306  bool FirstCall = false;
1307 
1308  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1309  new_done = CheckpointerShmem->ckpt_done;
1310  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1311 
1312  if (new_done != ckpt_done)
1313  FirstCall = true;
1314 
1315  ckpt_done = new_done;
1316 
1317  return FirstCall;
1318 }
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8280
int slock_t
Definition: s_lock.h:934
PgStat_Counter m_buf_fsync_backend
Definition: pgstat.h:419
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:1080
void RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
Definition: sync.c:435
bool IsPostmasterEnvironment
Definition: globals.c:108
void CheckpointWriteDelay(int flags, double progress)
Definition: checkpointer.c:663
SyncRequestType type
Definition: checkpointer.c:111
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:814
#define SIGQUIT
Definition: win32_port.h:155
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:731
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define AllocSetContextCreate
Definition: memutils.h:170
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9409
#define DEBUG1
Definition: elog.h:25
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:8246
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1069
int64 pg_time_t
Definition: pgtime.h:23
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
Definition: xlog.c:8309
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
int wal_segment_size
Definition: xlog.c:112
static void ReqCheckpointHandler(SIGNAL_ARGS)
Definition: checkpointer.c:810
int XLogArchiveTimeout
Definition: xlog.c:90
MemoryContext hcxt
Definition: hsearch.h:78
#define SIGUSR1
Definition: win32_port.h:166
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:932
void CreateCheckPoint(int flags)
Definition: xlog.c:8542
#define SIGCHLD
Definition: win32_port.h:164
PGPROC * MyProc
Definition: proc.c:67
PgStat_Counter m_timed_checkpoints
Definition: pgstat.h:413
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2433
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:855
#define SpinLockInit(lock)
Definition: spin.h:60
#define Min(x, y)
Definition: c.h:911
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:429
static void HandleCheckpointerInterrupts(void)
Definition: checkpointer.c:525
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
bool CreateRestartPoint(int flags)
Definition: xlog.c:9082
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void CheckpointerShmemInit(void)
Definition: checkpointer.c:853
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:142
Size entrysize
Definition: hsearch.h:73
int CheckPointWarning
Definition: checkpointer.c:147
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:135
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
void ConditionVariableBroadcast(ConditionVariable *cv)
void proc_exit(int code)
Definition: ipc.c:104
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define MemSet(start, val, len)
Definition: c.h:962
#define kill(pid, sig)
Definition: win32_port.h:426
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
#define SIGPIPE
Definition: win32_port.h:159
#define SIGUSR2
Definition: win32_port.h:167
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7930
void SetLatch(Latch *latch)
Definition: latch.c:436
void FlushErrorState(void)
Definition: elog.c:1678
void ResetLatch(Latch *latch)
Definition: latch.c:519
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2818
Latch procLatch
Definition: proc.h:104
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void smgrcloseall(void)
Definition: smgr.c:286
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
ErrorContextCallback * error_context_stack
Definition: elog.c:91
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:222
PgStat_Counter m_requested_checkpoints
Definition: pgstat.h:414
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:636
void ConditionVariableInit(ConditionVariable *cv)
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: dynahash.c:208
void AtEOXact_SMgr(void)
Definition: smgr.c:655
void SignalHandlerForCrashExit(SIGNAL_ARGS)
Definition: interrupt.c:72
void AbsorbSyncRequests(void)
void pfree(void *pointer)
Definition: mcxt.c:1056
void ConditionVariableCancelSleep(void)
#define AmBackgroundWriterProcess()
Definition: miscadmin.h:411
#define ERROR
Definition: elog.h:43
#define AmCheckpointerProcess()
Definition: miscadmin.h:412
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
static bool CompactCheckpointerRequestQueue(void)
bool ForwardSyncRequest(const FileTag *ftag, SyncRequestType type)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11193
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define DEBUG2
Definition: elog.h:24
bool IsUnderPostmaster
Definition: globals.c:109
#define CHECKPOINT_REQUESTED
Definition: xlog.h:220
int CheckPointTimeout
Definition: checkpointer.c:146
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:212
#define SIGHUP
Definition: win32_port.h:154
unsigned int uint32
Definition: c.h:359
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1344
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:27
Definition: guc.h:72
ConditionVariable done_cv
Definition: checkpointer.c:128
int CheckPointSegments
Definition: xlog.c:125
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:104
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:138
#define SIG_IGN
Definition: win32_port.h:151
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:8326
void UnlockBuffers(void)
Definition: bufmgr.c:3584
int progress
Definition: pgbench.c:234
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define SpinLockRelease(lock)
Definition: spin.h:64
#define HASH_BLOBS
Definition: hsearch.h:88
bool ExitOnAnyError
Definition: globals.c:113
static void UpdateSharedMemoryConfig(void)
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
sigset_t BlockSig
Definition: pqsignal.c:22
void UpdateFullPageWrites(void)
Definition: xlog.c:9511
void * palloc0(Size size)
Definition: mcxt.c:980
bool FirstCallSinceLastCheckpoint(void)
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define WRITES_PER_ABSORB
Definition: checkpointer.c:141
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
Size keysize
Definition: hsearch.h:72
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:834
void EmitErrorReport(void)
Definition: elog.c:1533
static pg_time_t last_xlog_switch_time
Definition: checkpointer.c:161
void pgstat_send_bgwriter(void)
Definition: pgstat.c:4388
void CheckpointerMain(void)
Definition: checkpointer.c:183
static bool ckpt_active
Definition: checkpointer.c:153
static pg_time_t ckpt_start_time
Definition: checkpointer.c:156
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:157
#define CHECKPOINT_WAIT
Definition: xlog.h:219
Latch * checkpointerLatch
Definition: proc.h:267
#define Max(x, y)
Definition: c.h:905
#define SIG_DFL
Definition: win32_port.h:149
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define SIGNAL_ARGS
Definition: c.h:1287
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:739
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:35
PgStat_Counter m_buf_written_backend
Definition: pgstat.h:418
size_t Size
Definition: c.h:467
#define SIGALRM
Definition: win32_port.h:160
#define MAX_SIGNAL_TRIES
SyncRequestType
Definition: sync.h:23
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
void AbortBufferIO(void)
Definition: bufmgr.c:4040
static double elapsed_time(instr_time *starttime)
Definition: explain.c:990
sigjmp_buf * PG_exception_stack
Definition: elog.c:93
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
static double ckpt_cached_elapsed
Definition: checkpointer.c:158
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:575
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:228
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:223
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:214
int NBuffers
Definition: globals.c:131
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:533
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1834
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:160
#define WL_LATCH_SET
Definition: latch.h:124
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
Definition: sync.h:45
#define offsetof(type, field)
Definition: c.h:662
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
void RequestCheckpoint(int flags)
Definition: checkpointer.c:896
double CheckPointCompletionTarget
Definition: checkpointer.c:148
ConditionVariable start_cv
Definition: checkpointer.c:127