Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL Source Code interpretation (214)-background process # 13 (checkpointer-IsCheckpointOnSchedule)

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This section introduces the function used to control the frequency of checkpoint flushing in checkpoint: IsCheckpointOnSchedule.

I. data structure

Macro definition

Checkpoints request flag bits

Checkpoints request flag bits, checkpoint request tag bit definition.

/ * OR-able request flag bits for checkpoints. The "cause" bits are used only * for logging purposes. Note: the flags must be defined so that it's * sensible to OR together request flags arising from different requestors. * / / * These directly affect the behavior of CreateCheckPoint and subsidiaries * / # define CHECKPOINT_IS_SHUTDOWN 0x0001 / * Checkpoint is for shutdown * / # define CHECKPOINT_END_OF_RECOVERY 0x0002 / * Like shutdown checkpoint, but * issued at end of WAL recovery * / # define CHECKPOINT_IMMEDIATE 0x0004 / * Do it without delays * / # define CHECKPOINT_FORCE 0x0008 / * Force even if no activity * / # define CHECKPOINT_FLUSH_ALL 0x0010 / * Flush all pages Including those * belonging to unlogged tables * / / * These are important to RequestCheckpoint * / # define CHECKPOINT_WAIT 0x0020 / * Wait for completion * / # define CHECKPOINT_REQUESTED 0x0040 / * Checkpoint request has been made * / / * These indicate the cause of a checkpoint request * / # define CHECKPOINT_CAUSE_XLOG 0x0080 / * XLOG consumption * / # define CHECKPOINT_CAUSE_TIME 0x0100 / * Elapsed time * /

WRITES_PER_ABSORB

/ * interval for calling AbsorbSyncRequests in CheckpointWriteDelay * / / interval between calls to AbsorbSyncRequests. Default is 1000#define WRITES_PER_ABSORB 1000. II. Source code interpretation

IsCheckpointOnSchedule

This function determines whether you are completing the scheduling of checkpoint, and you can rest if you return T, otherwise you need to work if you return F.

/ * * Calculate CheckPointSegments based on max_wal_size_mb and * checkpoint_completion_target. * calculate CheckPointSegments * / static voidCalculateCheckpointSegments (void) {double target; / *-* Calculate the distance at which to trigger a checkpoint, to avoid * exceeding max_wal_size_mb. This is based on two assumptions: * * a) we keep WAL for only one checkpoint cycle (prior to PG11 we kept * WAL for two checkpoint cycles to allow us to recover from the * secondary checkpoint if the first checkpoint failed, though we * only did this on the master anyway, not on standby. Keeping just * one checkpoint simplifies processing and reduces disk space in * many smaller databases.) * b) during checkpoint, we consume checkpoint_completion_target * * number of segments consumed between checkpoints. *-* / / # define ConvertToXSegs (x segsize) (x / (segsize) / (1024 * 1024)) target = (double) ConvertToXSegs (max_wal_size_mb, wal_segment_size) / (1.0 + CheckPointCompletionTarget); / * round down * / CheckPointSegments = (int) target; if (CheckPointSegments < 1) CheckPointSegments = 1 } / * * IsCheckpointOnSchedule-- are we on schedule to finish this checkpoint * (or restartpoint) in time? * IsCheckpointOnSchedule-- whether you are completing the scheduling of checkpoint * * Compares the current progress against the time/segments elapsed since last * checkpoint, and returns true if the progress we've made this far is greater * than the elapsed time/segments. * compare the current progress with the lost time/xlog segments. If the progress is early, return T (enter the rest state) * / static boolIsCheckpointOnSchedule (double progress) {XLogRecPtr recptr; struct timeval now; double elapsed_xlogs, elapsed_time; Assert (ckpt_active); / * Scale progress according to checkpoint_completion_target. * / / the actual progress is adjusted to progress*checkpoint_completion_target progress* = CheckPointCompletionTarget; / * * Check against the cached value first. Only do the more expensive * calculations once we reach the target previously calculated. Since * neither time or WAL insert pointer moves backwards, a freshly * calculated value can only be greater than or equal to the cached value. * if the progress is less than the cache value, return F to speed up the progress! * / if (progress < ckpt_cached_elapsed) return false; / * * Check progress against WAL segments written and CheckPointSegments. * Progress vs WAL * * We compare the current WAL insert location against the location * computed before calling CreateCheckPoint. The code in XLogInsert that * actually triggers a checkpoint when CheckPointSegments is exceeded * compares against RedoRecptr, so this is not completely accurate. * However, it's good enough for our purposes, we're only calculating an * estimate anyway. * During recovery, we compare last replayed WAL record's location with * the location computed before calling CreateRestartPoint. That maintains * the same pacing as we have during checkpoints in normal operation, but * we might exceed max_wal_size by a fair amount. That's because there can * be a large gap between a checkpoint's redo-pointer and the checkpoint * record itself, and we only start the restartpoint after we've seen the * checkpoint record. (The gap is typically up to CheckPointSegments * * checkpoint_completion_target where checkpoint_completion_target is the * value that was in effect when the WAL was generated) * / if (RecoveryInProgress ()) recptr = GetXLogReplayRecPtr (NULL); else recptr = GetInsertRecPtr (); elapsed_xlogs = (double) (recptr-ckpt_start_recptr)) / wal_segment_size) / CheckPointSegments; if (progress < elapsed_xlogs) {/ / the progress is less than the speed of generating xlogs. You need to work ckpt_cached_elapsed = elapsed_xlogs; return false;} / * * Check progress against time elapsed and checkpoint_timeout. * compare time * / gettimeofday (& now, NULL); elapsed_time = ((double) ((pg_time_t) now.tv_sec-ckpt_start_time) + now.tv_usec / 1000000.0) / CheckPointTimeout; if (progress < elapsed_time) {/ / the progress is slower than the elapsed time, and you need to work ckpt_cached_elapsed = elapsed_time; return false;} / * It looks like we're on schedule. * / in scheduling, you can rest return true;} 3. Follow-up and analysis

N/A

IV. Reference materials

PG Source Code

Analysis of the characteristics of PgSQL and the scheduling of checkpoint

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report