1/*-------------------------------------------------------------------------
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
13 * The normal termination sequence is that checkpointer is instructed to
14 * execute the shutdown checkpoint by SIGINT. After that checkpointer waits
15 * to be terminated via SIGUSR2, which instructs the checkpointer to exit(0).
16 * All backends must be stopped before SIGINT or SIGUSR2 is issued!
18 * Emergency termination is by SIGQUIT; like any backend, the checkpointer
19 * will simply abort and exit on SIGQUIT.
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.)
29 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
33 * src/backend/postmaster/checkpointer.c
35 *-------------------------------------------------------------------------
73 * Shared memory area for communication between checkpointer and backends
75 * The ckpt counters allow backends to watch for completion of a checkpoint
76 * request they send. Here's how it works:
77 * * At start of a checkpoint, checkpointer reads (and clears) the request
78 * flags and increments ckpt_started, while holding ckpt_lck.
79 * * On completion of a checkpoint, checkpointer sets ckpt_done to
81 * * On failure of a checkpoint, checkpointer increments ckpt_failed
82 * and sets ckpt_done to equal ckpt_started.
84 * The algorithm for backends is:
85 * 1. Record current values of ckpt_failed and ckpt_started, and
86 * set request flags, while holding ckpt_lck.
87 * 2. Send signal to request checkpoint.
88 * 3. Sleep until ckpt_started changes. Now you know a checkpoint has
89 * begun since you started this algorithm (although *not* that it was
90 * specifically initiated by your signal), and that it is using your flags.
91 * 4. Record new value of ckpt_started.
92 * 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
93 * arithmetic here in case counters wrap around.) Now you know a
94 * checkpoint has started and completed, but not whether it was
96 * 6. If ckpt_failed is different from the originally saved value,
97 * assume request failed; otherwise it was definitely successful.
99 * ckpt_flags holds the OR of the checkpoint request flags sent by all
100 * requesting backends since the last checkpoint start. The flags are
101 * chosen so that OR'ing is the correct way to combine multiple requests.
103 * The requests array holds fsync requests sent by backends and not yet
104 * absorbed by the checkpointer.
106 * Unlike the checkpoint fields, requests related fields are protected by
107 * CheckpointerCommLock.
120 slock_t
ckpt_lck;
/* protects all the ckpt_* fields */
134 int head;
/* Index of the first request in the ring
136 int tail;
/* Index of the last request in the ring
139 /* The ring buffer of pending checkpointer requests */
145/* interval for calling AbsorbSyncRequests in CheckpointWriteDelay */
146 #define WRITES_PER_ABSORB 1000
148/* Maximum number of checkpointer requests to process in one batch */
149 #define CKPT_REQ_BATCH_SIZE 10000
151/* Max number of requests the checkpointer request queue can hold */
152 #define MAX_CHECKPOINT_REQUESTS 10000000
167/* these values are valid when ckpt_active is true: */
175/* Prototypes for private functions */
189 * Main entry point for checkpointer process
191 * This is invoked from AuxiliaryProcessMain, which has already created the
192 * basic execution environment, but not enabled signals yet.
197 sigjmp_buf local_sigjmp_buf;
200 Assert(startup_data_len == 0);
208 * Properly accept or ignore signals the postmaster might send us
210 * Note: we deliberately ignore SIGTERM, because during a standard Unix
211 * system shutdown cycle, init will SIGTERM all processes at once. We
212 * want to wait for the backends to exit, whereupon the postmaster will
213 * tell us it's okay to shut down (via SIGUSR2).
217 pqsignal(SIGTERM, SIG_IGN);
/* ignore SIGTERM */
218 /* SIGQUIT handler was already set up by InitPostmasterChild */
225 * Reset some signals that are accepted by postmaster but not here
230 * Initialize so that first time-driven event happens at the correct time.
235 * Write out stats after shutdown. This needs to be called by exactly one
236 * process during a normal shutdown, and since checkpointer is shut down
239 * While e.g. walsenders are active after the shutdown checkpoint has been
240 * written (and thus could produce more stats), checkpointer stays around
241 * after the shutdown checkpoint has been written. postmaster will only
242 * signal checkpointer to exit after all processes that could emit stats
243 * have been shut down.
248 * Create a memory context that we will do all our work in. We do this so
249 * that we can reset the context during error recovery and thereby avoid
250 * possible memory leaks. Formerly this code just ran in
251 * TopMemoryContext, but resetting that would be a really bad idea.
259 * If an exception is encountered, processing resumes here.
261 * You might wonder why this isn't coded as an infinite loop around a
262 * PG_TRY construct. The reason is that this is the bottom of the
263 * exception stack, and so with PG_TRY there would be no exception handler
264 * in force at all during the CATCH part. By leaving the outermost setjmp
265 * always active, we have at least some chance of recovering from an error
266 * during error recovery. (If we get into an infinite loop thereby, it
267 * will soon be stopped by overflow of elog.c's internal state stack.)
269 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
270 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
271 * signals other than SIGQUIT will be blocked until we complete error
272 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
273 * call redundant, but it is not since InterruptPending might be set
276 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
278 /* Since not using PG_TRY, must reset error stack by hand */
281 /* Prevent interrupts while cleaning up */
284 /* Report the error to the server log */
288 * These operations are really just a minimal subset of
289 * AbortTransaction(). We don't have very many resources to worry
290 * about in checkpointer, but we do have LWLocks, buffers, and temp
304 /* Warn any waiting backends that the checkpoint failed. */
318 * Now return to normal top-level context and clear ErrorContext for
324 /* Flush any leaked data in the top-level context */
327 /* Now we can allow interrupts again */
331 * Sleep at least 1 second after any error. A write error is likely
332 * to be repeated, and we don't want to be filling the error logs as
338 /* We can now handle ereport(ERROR) */
342 * Unblock signals (they were blocked when the postmaster forked us)
347 * Ensure all shared memory values are set correctly for the config. Doing
348 * this here ensures no race conditions from other concurrent updaters.
353 * Advertise our proc number that backends can use to wake us up while
359 * Loop until we've been asked to write the shutdown checkpoint or
364 bool do_checkpoint =
false;
369 bool chkpt_or_rstpt_requested =
false;
370 bool chkpt_or_rstpt_timed =
false;
372 /* Clear any already-pending wakeups */
376 * Process any requests or signals received recently.
385 * Detect a pending checkpoint request by checking whether the flags
386 * word in shared memory is nonzero. We shouldn't need to acquire the
391 do_checkpoint =
true;
392 chkpt_or_rstpt_requested =
true;
396 * Force a checkpoint if too much time has elapsed since the last one.
397 * Note that we count a timed checkpoint in stats only when this
398 * occurs without an external request, but we set the CAUSE_TIME flag
399 * bit even if there is also an external request.
406 chkpt_or_rstpt_timed =
true;
407 do_checkpoint =
true;
412 * Do a checkpoint if requested.
416 bool ckpt_performed =
false;
417 bool do_restartpoint;
419 /* Check if we should perform a checkpoint or a restartpoint. */
423 * Atomically fetch the request flags to figure out what kind of a
424 * checkpoint we should perform, and increase the started-counter
425 * to acknowledge that we've started a new checkpoint.
436 * The end-of-recovery checkpoint is a real checkpoint that's
437 * performed while we're still in recovery.
440 do_restartpoint =
false;
442 if (chkpt_or_rstpt_timed)
444 chkpt_or_rstpt_timed =
false;
451 if (chkpt_or_rstpt_requested)
453 chkpt_or_rstpt_requested =
false;
461 * We will warn if (a) too soon since last checkpoint (whatever
462 * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
463 * since the last checkpoint start. Note in particular that this
464 * implementation will not generate warnings caused by
465 * CheckPointTimeout < CheckPointWarning.
467 if (!do_restartpoint &&
471 (
errmsg_plural(
"checkpoints are occurring too frequently (%d second apart)",
472 "checkpoints are occurring too frequently (%d seconds apart)",
475 errhint(
"Consider increasing the configuration parameter \"%s\".",
"max_wal_size")));
478 * Initialize checkpointer-private variables used during
492 if (!do_restartpoint)
498 * After any checkpoint, free all smgr objects. Otherwise we
499 * would never do so for dropped relations, as the checkpointer
500 * does not process shared invalidation messages or call
506 * Indicate checkpoint completion to any waiting backends.
514 if (!do_restartpoint)
517 * Note we record the checkpoint start time not end time as
518 * last_checkpoint_time. This is so that time-driven
519 * checkpoints happen at a predictable spacing.
531 * The same as for checkpoint. Please see the
532 * corresponding comment.
541 * We were not able to perform the restartpoint
542 * (checkpoints throw an ERROR in case of error). Most
543 * likely because we have not received any new checkpoint
544 * WAL records since the last restartpoint. Try again in
554 * We may have received an interrupt during the checkpoint and the
555 * latch might have been reset (e.g. in CheckpointWriteDelay).
562 /* Check for archive_timeout and switch xlog files if necessary. */
565 /* Report pending statistics to the cumulative stats system */
570 * If any checkpoint flags have been set, redo the loop to handle the
571 * checkpoint without sleeping.
577 * Sleep until we are signaled or it's time for another checkpoint or
583 continue;
/* no sleep for us ... */
589 continue;
/* no sleep for us ... */
595 cur_timeout * 1000L
/* convert to ms */ ,
596 WAIT_EVENT_CHECKPOINTER_MAIN);
600 * From here on, elog(ERROR) should end with exit(1), not send control
601 * back to the sigsetjmp block above.
608 * Close down the database.
610 * Since ShutdownXLOG() creates restartpoint or checkpoint, and
611 * updates the statistics, increment the checkpoint request and flush
612 * out pending statistic.
620 * Tell postmaster that we're done.
627 * Wait until we're asked to shut down. By separating the writing of the
628 * shutdown checkpoint from checkpointer exiting, checkpointer can perform
629 * some should-be-as-late-as-possible work like writing out stats.
633 /* Clear any already-pending wakeups */
644 WAIT_EVENT_CHECKPOINTER_SHUTDOWN);
647 /* Normal exit from the checkpointer is here */
652 * Process any new interrupts.
666 * Checkpointer is the last process to shut down, so we ask it to hold
667 * the keys for a range of other tasks required most of which have
668 * nothing to do with checkpointing at all.
670 * For various reasons, some config values can change dynamically so
671 * the primary copy of them is held in shared memory to make sure all
672 * backends see the same value. We make Checkpointer responsible for
673 * updating the shared memory copy if the parameter setting changes
679 /* Perform logging of memory contexts of this process */
685 * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
687 * This will switch to a new WAL file and force an archive file write if
688 * meaningful activity is recorded in the current WAL file. This includes most
689 * writes, including just a single checkpoint record, but excludes WAL records
690 * that were inserted with the XLOG_MARK_UNIMPORTANT flag being set (like
691 * snapshots of running transactions). Such records, depending on
692 * configuration, occur on regular intervals and don't contain important
693 * information. This avoids generating archives with a few unimportant
708 /* First we do a quick check using possibly-stale local state. */
713 * Update local state ... note that last_xlog_switch_time is the last time
714 * a switch was performed *or requested*.
720 /* Now we can do the real checks */
724 * Switch segment only when "important" WAL has been logged since the
725 * last segment switch (last_switch_lsn points to end of segment
726 * switch occurred in).
732 /* mark switch as unimportant, avoids triggering checkpoints */
736 * If the returned pointer points exactly to a segment boundary,
737 * assume nothing happened.
740 elog(
DEBUG1,
"write-ahead log switch forced (\"archive_timeout\"=%d)",
745 * Update state in any case, so we don't retry constantly when the
753 * Returns true if a fast checkpoint request is pending. (Note that this does
754 * not check the *current* checkpoint's FAST flag, but whether there is one
755 * pending behind it.)
763 * We don't need to acquire the ckpt_lck in this case because we're only
764 * looking at a single flag bit.
772 * CheckpointWriteDelay -- control rate of checkpoint
774 * This function is called after each page write performed by BufferSync().
775 * It is responsible for throttling BufferSync()'s write rate to hit
776 * checkpoint_completion_target.
778 * The checkpoint request flags should be passed in; currently the only one
779 * examined is CHECKPOINT_FAST, which disables delays between writes.
781 * 'progress' is an estimate of how much of the work has been done, as a
782 * fraction between 0.0 meaning none, and 1.0 meaning all done.
789 /* Do nothing if checkpoint is being executed by non-checkpointer process */
794 * Perform the usual duties and take a nap, unless we're behind schedule,
795 * in which case we just try to catch up as quickly as possible.
807 /* update shmem copies of config variables */
816 /* Report interim statistics to the cumulative stats system */
820 * This sleep used to be connected to bgwriter_delay, typically 200ms.
821 * That resulted in more frequent wakeups if not much work to do.
822 * Checkpointer and bgwriter are no longer related so take the Big
827 WAIT_EVENT_CHECKPOINT_WRITE_DELAY);
830 else if (--absorb_counter <= 0)
833 * Absorb pending fsync requests after each WRITES_PER_ABSORB write
834 * operations even when we don't sleep, to prevent overflow of the
835 * fsync request queue.
841 /* Check for barrier events. */
847 * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
848 * (or restartpoint) in time?
850 * Compares the current progress against the time/segments elapsed since last
851 * checkpoint, and returns true if the progress we've made this far is greater
852 * than the elapsed time/segments.
859 double elapsed_xlogs,
864 /* Scale progress according to checkpoint_completion_target. */
868 * Check against the cached value first. Only do the more expensive
869 * calculations once we reach the target previously calculated. Since
870 * neither time or WAL insert pointer moves backwards, a freshly
871 * calculated value can only be greater than or equal to the cached value.
877 * Check progress against WAL segments written and CheckPointSegments.
879 * We compare the current WAL insert location against the location
880 * computed before calling CreateCheckPoint. The code in XLogInsert that
881 * actually triggers a checkpoint when CheckPointSegments is exceeded
882 * compares against RedoRecPtr, so this is not completely accurate.
883 * However, it's good enough for our purposes, we're only calculating an
886 * During recovery, we compare last replayed WAL record's location with
887 * the location computed before calling CreateRestartPoint. That maintains
888 * the same pacing as we have during checkpoints in normal operation, but
889 * we might exceed max_wal_size by a fair amount. That's because there can
890 * be a large gap between a checkpoint's redo-pointer and the checkpoint
891 * record itself, and we only start the restartpoint after we've seen the
892 * checkpoint record. (The gap is typically up to CheckPointSegments *
893 * checkpoint_completion_target where checkpoint_completion_target is the
894 * value that was in effect when the WAL was generated).
910 * Check progress against time elapsed and checkpoint_timeout.
922 /* It looks like we're on schedule. */
927/* --------------------------------
928 * signal handler routines
929 * --------------------------------
932/* SIGINT: set flag to trigger writing of shutdown checkpoint */
941/* --------------------------------
942 * communication with backends
943 * --------------------------------
947 * CheckpointerShmemSize
948 * Compute space needed for checkpointer-related shared memory
956 * The size of the requests[] array is arbitrarily set equal to NBuffers.
957 * But there is a cap of MAX_CHECKPOINT_REQUESTS to prevent accumulating
958 * too many checkpoint requests in the ring buffer.
969 * CheckpointerShmemInit
970 * Allocate and initialize checkpointer-related shared memory
986 * First time through, so initialize. Note that we zero the whole
987 * requests array; this is so that CompactCheckpointerRequestQueue can
988 * assume that any pad bytes in the request structs are zeroes.
1001 * Primary entry point for manual CHECKPOINT commands
1003 * This is mainly a wrapper for RequestCheckpoint().
1009 bool unlogged =
false;
1013 if (strcmp(opt->defname,
"mode") == 0)
1017 if (strcmp(
mode,
"spread") == 0)
1019 else if (strcmp(
mode,
"fast") != 0)
1021 (
errcode(ERRCODE_SYNTAX_ERROR),
1022 errmsg(
"unrecognized MODE option \"%s\"",
mode),
1025 else if (strcmp(opt->defname,
"flush_unlogged") == 0)
1029 (
errcode(ERRCODE_SYNTAX_ERROR),
1030 errmsg(
"unrecognized CHECKPOINT option \"%s\"", opt->defname),
1036 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1037 /* translator: %s is name of an SQL command (e.g., CHECKPOINT) */
1038 errmsg(
"permission denied to execute %s command",
1040 errdetail(
"Only roles with privileges of the \"%s\" role may execute this command.",
1051 * Called in backend processes to request a checkpoint
1053 * flags is a bitwise OR of the following:
1054 * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
1055 * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
1056 * CHECKPOINT_FAST: finish the checkpoint ASAP,
1057 * ignoring checkpoint_completion_target parameter.
1058 * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred
1059 * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
1060 * CHECKPOINT_END_OF_RECOVERY, and the CHECKPOINT command).
1061 * CHECKPOINT_WAIT: wait for completion before returning (otherwise,
1062 * just signal checkpointer to do it, and return).
1063 * CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
1064 * (This affects logging, and in particular enables CheckPointWarning.)
1074 * If in a standalone backend, just do it ourselves.
1079 * There's no point in doing slow checkpoints in a standalone backend,
1080 * because there's no other backends the checkpoint could disrupt.
1084 /* Free all smgr objects, as CheckpointerMain() normally would. */
1091 * Atomically set the request flags, and take a snapshot of the counters.
1092 * When we see ckpt_started > old_started, we know the flags we set here
1093 * have been seen by checkpointer.
1095 * Note that we OR the flags with any existing flags, to avoid overriding
1096 * a "stronger" request by another backend. The flag senses must be
1097 * chosen to make this work!
1108 * Set checkpointer's latch to request checkpoint. It's possible that the
1109 * checkpointer hasn't started yet, so we will retry a few times if
1110 * needed. (Actually, more than a few times, since on slow or overloaded
1111 * buildfarm machines, it's been observed that the checkpointer can take
1112 * several seconds to start.) However, if not told to wait for the
1113 * checkpoint to occur, we consider failure to set the latch to be
1114 * nonfatal and merely LOG it. The checkpointer should see the request
1115 * when it does start, with or without the SetLatch().
1117#define MAX_SIGNAL_TRIES 600 /* max wait 60.0 sec */
1118 for (ntries = 0;; ntries++)
1128 "could not notify checkpoint: checkpointer is not running");
1135 /* notified successfully */
1140 pg_usleep(100000L);
/* wait 0.1 sec, then retry */
1144 * If requested, wait for completion. We detect completion according to
1145 * the algorithm given above.
1152 /* Wait for a new checkpoint to start. */
1160 if (new_started != old_started)
1164 WAIT_EVENT_CHECKPOINT_START);
1169 * We are waiting for ckpt_done >= new_started, in a modulo sense.
1181 if (new_done - new_started >= 0)
1185 WAIT_EVENT_CHECKPOINT_DONE);
1189 if (new_failed != old_failed)
1191 (
errmsg(
"checkpoint request failed"),
1192 errhint(
"Consult recent messages in the server log for details.")));
1197 * ForwardSyncRequest
1198 * Forward a file-fsync request from a backend to the checkpointer
1200 * Whenever a backend is compelled to write directly to a relation
1201 * (which should be seldom, if the background writer is getting its job done),
1202 * the backend calls this routine to pass over knowledge that the relation
1203 * is dirty and must be fsync'd before next checkpoint. We also use this
1204 * opportunity to count such writes for statistical purposes.
1206 * To avoid holding the lock for longer than necessary, we normally write
1207 * to the requests[] queue without checking for duplicates. The checkpointer
1208 * will have to eliminate dups internally anyway. However, if we discover
1209 * that the queue is full, we make a pass over the entire queue to compact
1210 * it. This is somewhat expensive, but the alternative is for the backend
1211 * to perform its own fsync, which is far more expensive in practice. It
1212 * is theoretically possible a backend fsync might still be necessary, if
1213 * the queue is full and contains no duplicate entries. In that case, we
1214 * let the backend know by returning false.
1224 return false;
/* probably shouldn't even get here */
1227 elog(
ERROR,
"ForwardSyncRequest must not be called in checkpointer");
1232 * If the checkpointer isn't running or the request queue is full, the
1233 * backend will have to perform its own fsync request. But before forcing
1234 * that to happen, we can try to compact the request queue.
1244 /* OK, insert request */
1247 request->
ftag = *ftag;
1253 /* If queue is more than half full, nudge the checkpointer to empty it */
1259 /* ... but not till after we release the lock */
1273 * CompactCheckpointerRequestQueue
1274 * Remove duplicates from the request queue to avoid backend fsyncs.
1275 * Returns "true" if any entries were removed.
1277 * Although a full fsync request queue is not common, it can lead to severe
1278 * performance problems when it does happen. So far, this situation has
1279 * only been observed to occur when the system is under heavy write load,
1280 * and especially during the "sync" phase of a checkpoint. Without this
1281 * logic, each backend begins doing an fsync for every block written, which
1282 * gets very expensive and can slow down the whole system.
1284 * Trying to do this every time the queue is full could lose if there
1285 * aren't any removable entries. But that should be vanishingly rare in
1286 * practice: there's one queue entry per shared buffer.
1291 struct CheckpointerSlotMapping
1298 int num_skipped = 0;
1308 /* must hold CheckpointerCommLock in exclusive mode */
1311 /* Avoid memory allocations in a critical section. */
1318 /* Initialize skip_slot array */
1319 skip_slot =
palloc0(
sizeof(
bool) * max_requests);
1323 /* Initialize temporary hash table */
1325 ctl.entrysize =
sizeof(
struct CheckpointerSlotMapping);
1328 htab =
hash_create(
"CompactCheckpointerRequestQueue",
1334 * The basic idea here is that a request can be skipped if it's followed
1335 * by a later, identical request. It might seem more sensible to work
1336 * backwards from the end of the queue and check whether a request is
1337 * *preceded* by an earlier, identical request, in the hopes of doing less
1338 * copying. But that might change the semantics, if there's an
1339 * intervening SYNC_FORGET_REQUEST or SYNC_FILTER_REQUEST, so we do it
1340 * this way. It would be possible to be even smarter if we made the code
1341 * below understand the specific semantics of such requests (it could blow
1342 * away preceding entries that would end up being canceled anyhow), but
1343 * it's not clear that the extra complexity would buy us anything.
1346 for (n = 0; n < num_requests; n++)
1349 struct CheckpointerSlotMapping *slotmap;
1353 * We use the request struct directly as a hashtable key. This
1354 * assumes that any padding bytes in the structs are consistently the
1355 * same, which should be okay because we zeroed them in
1356 * CheckpointerShmemInit. Note also that RelFileLocator had better
1357 * contain no pad bytes.
1363 /* Duplicate, so mark the previous occurrence as skippable */
1364 skip_slot[slotmap->ring_idx] =
true;
1367 /* Remember slot containing latest occurrence of this request value */
1368 slotmap->ring_idx = read_idx;
1370 /* Move to the next request in the ring buffer */
1371 read_idx = (read_idx + 1) % max_requests;
1374 /* Done with the hash table. */
1377 /* If no duplicates, we're out of luck. */
1384 /* We found some duplicates; remove them. */
1385 read_idx = write_idx = head;
1386 for (n = 0; n < num_requests; n++)
1388 /* If this slot is NOT skipped, keep it */
1389 if (!skip_slot[read_idx])
1391 /* If the read and write positions are different, copy the request */
1392 if (write_idx != read_idx)
1396 /* Advance the write position */
1397 write_idx = (write_idx + 1) % max_requests;
1400 read_idx = (read_idx + 1) % max_requests;
1404 * Update ring buffer state: head remains the same, tail moves, count
1411 (
errmsg_internal(
"compacted fsync request queue from %d entries to %d entries",
1420 * AbsorbSyncRequests
1421 * Retrieve queued sync requests and pass them to sync mechanism.
1423 * This is exported because it must be called during CreateCheckPoint;
1424 * we have to be sure we have accepted all pending requests just before
1425 * we start fsync'ing. Since CreateCheckPoint sometimes runs in
1426 * non-checkpointer processes, do nothing if not checkpointer.
1445 * We try to avoid holding the lock for a long time by:
1446 * 1. Copying the request array and processing the requests after
1447 * releasing the lock;
1448 * 2. Processing not the whole queue, but only batches of
1449 * CKPT_REQ_BATCH_SIZE at once.
1451 * Once we have cleared the requests from shared memory, we must
1452 * PANIC if we then fail to absorb them (e.g., because our hashtable
1453 * runs out of memory). This is because the system cannot run safely
1454 * if we are unable to fsync what we have been told to fsync.
1455 * Fortunately, the hashtable is so small that the problem is quite
1456 * unlikely to arise in practice.
1458 * Note: The maximum possible size of a ring buffer is
1459 * MAX_CHECKPOINT_REQUESTS entries, which fit into a maximum palloc
1460 * allocation size of 1Gb. Our maximum batch size,
1461 * CKPT_REQ_BATCH_SIZE, is even smaller.
1469 for (
i = 0;
i < n;
i++)
1481 /* Are there any requests in the queue? If so, keep going. */
1486 for (request = requests; n > 0; request++, n--)
1497 * Update any shared memory configurations based on config parameters
1502 /* update global shmem state for sync rep */
1506 * If full_page_writes has been changed by SIGHUP, we update it in shared
1507 * memory and write an XLOG_FPW_CHANGE record.
1511 elog(
DEBUG2,
"checkpointer updated shared memory configuration values");
1515 * FirstCallSinceLastCheckpoint allows a process to take an action once
1516 * per checkpoint cycle by asynchronously checking for checkpoint completion.
1521 static int ckpt_done = 0;
1523 bool FirstCall =
false;
1529 if (new_done != ckpt_done)
1532 ckpt_done = new_done;
bool has_privs_of_role(Oid member, Oid role)
void pgaio_error_cleanup(void)
void AuxiliaryProcessMainCommon(void)
Datum now(PG_FUNCTION_ARGS)
void AtEOXact_Buffers(bool isCommit)
#define FLEXIBLE_ARRAY_MEMBER
#define MemSet(start, val, len)
static void UpdateSharedMemoryConfig(void)
static bool FastCheckpointRequested(void)
static XLogRecPtr ckpt_start_recptr
static bool IsCheckpointOnSchedule(double progress)
bool ForwardSyncRequest(const FileTag *ftag, SyncRequestType type)
static void ReqShutdownXLOG(SIGNAL_ARGS)
static void CheckArchiveTimeout(void)
static double ckpt_cached_elapsed
void CheckpointerMain(const void *startup_data, size_t startup_data_len)
static bool CompactCheckpointerRequestQueue(void)
static void ProcessCheckpointerInterrupts(void)
static volatile sig_atomic_t ShutdownXLOGPending
#define CKPT_REQ_BATCH_SIZE
void AbsorbSyncRequests(void)
#define WRITES_PER_ABSORB
double CheckPointCompletionTarget
static pg_time_t last_xlog_switch_time
#define MAX_CHECKPOINT_REQUESTS
void CheckpointerShmemInit(void)
bool FirstCallSinceLastCheckpoint(void)
static CheckpointerShmemStruct * CheckpointerShmem
void RequestCheckpoint(int flags)
static pg_time_t last_checkpoint_time
void ExecCheckpoint(ParseState *pstate, CheckPointStmt *stmt)
void CheckpointWriteDelay(int flags, double progress)
static pg_time_t ckpt_start_time
Size CheckpointerShmemSize(void)
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableInit(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
char * defGetString(DefElem *def)
bool defGetBoolean(DefElem *def)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
void AtEOXact_HashTables(bool isCommit)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
void hash_destroy(HTAB *hashp)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errmsg_internal(const char *fmt,...)
void EmitErrorReport(void)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
void FlushErrorState(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
sigjmp_buf * PG_exception_stack
#define ereport(elevel,...)
static double elapsed_time(instr_time *starttime)
void AtEOXact_Files(bool isCommit)
volatile sig_atomic_t LogMemoryContextPending
volatile sig_atomic_t ProcSignalBarrierPending
volatile uint32 CritSectionCount
bool IsPostmasterEnvironment
void ProcessConfigFile(GucContext context)
Assert(PointerIsAligned(start, uint64))
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
volatile sig_atomic_t ShutdownRequestPending
volatile sig_atomic_t ConfigReloadPending
void SignalHandlerForConfigReload(SIGNAL_ARGS)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
void SetLatch(Latch *latch)
void ResetLatch(Latch *latch)
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
bool LWLockHeldByMe(LWLock *lock)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void LWLockReleaseAll(void)
void MemoryContextReset(MemoryContext context)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext TopMemoryContext
MemoryContext CurrentMemoryContext
void ProcessLogMemoryContextInterrupt(void)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define AmCheckpointerProcess()
#define RESUME_INTERRUPTS()
#define START_CRIT_SECTION()
#define CHECK_FOR_INTERRUPTS()
#define HOLD_INTERRUPTS()
#define END_CRIT_SECTION()
BackendType MyBackendType
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
int parser_errposition(ParseState *pstate, int location)
static PgChecksumMode mode
#define foreach_ptr(type, var, lst)
void pgstat_before_server_shutdown(int code, Datum arg)
void pgstat_report_checkpointer(void)
PgStat_CheckpointerStats PendingCheckpointerStats
void pgstat_report_wal(bool force)
void SendPostmasterSignal(PMSignalReason reason)
@ PMSIGNAL_XLOG_IS_SHUTDOWN
#define GetPGProcByNumber(n)
#define INVALID_PROC_NUMBER
void ProcessProcSignalBarrier(void)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
void ReleaseAuxProcessResources(bool isCommit)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
void pg_usleep(long microsec)
void smgrdestroyall(void)
#define SpinLockInit(lock)
#define SpinLockRelease(lock)
#define SpinLockAcquire(lock)
ConditionVariable done_cv
ConditionVariable start_cv
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
ProcNumber checkpointerProc
PgStat_Counter restartpoints_requested
PgStat_Counter num_requested
PgStat_Counter num_performed
PgStat_Counter restartpoints_timed
PgStat_Counter restartpoints_performed
void RememberSyncRequest(const FileTag *ftag, SyncRequestType type)
void SyncRepUpdateSyncStandbysDefined(void)
static void pgstat_report_wait_end(void)
#define WL_EXIT_ON_PM_DEATH
int gettimeofday(struct timeval *tp, void *tzp)
void UpdateFullPageWrites(void)
bool RecoveryInProgress(void)
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
bool CreateRestartPoint(int flags)
XLogRecPtr GetInsertRecPtr(void)
void ShutdownXLOG(int code, Datum arg)
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
XLogRecPtr GetLastImportantRecPtr(void)
bool CreateCheckPoint(int flags)
#define CHECKPOINT_FLUSH_UNLOGGED
#define CHECKPOINT_CAUSE_XLOG
#define CHECKPOINT_END_OF_RECOVERY
#define CHECKPOINT_CAUSE_TIME
#define CHECKPOINT_REQUESTED
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)