Skip to content

Commit 18fb9d8

Browse files
Reduce checkpoints and WAL traffic on low activity database server
Previously, we skipped a checkpoint if no WAL had been written since last checkpoint, though this does not appear in user documentation. As of now, we skip a checkpoint until we have written at least one enough WAL to switch the next WAL file. This greatly reduces the level of activity and number of WAL messages generated by a very low activity server. This is safe because the purpose of a checkpoint is to act as a starting place for a recovery, in case of crash. This patch maintains minimal WAL volume for replay in case of crash, thus maintaining very low crash recovery time.
1 parent 9aceb6a commit 18fb9d8

File tree

1 file changed

+15
-13
lines changed
  • src/backend/access/transam

1 file changed

+15
-13
lines changed

src/backend/access/transam/xlog.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7560,6 +7560,10 @@ CreateCheckPoint(int flags)
75607560
uint32 freespace;
75617561
uint32 _logId;
75627562
uint32 _logSeg;
7563+
uint32 redo_logId;
7564+
uint32 redo_logSeg;
7565+
uint32 insert_logId;
7566+
uint32 insert_logSeg;
75637567
TransactionId *inCommitXids;
75647568
int nInCommit;
75657569

@@ -7636,33 +7640,31 @@ CreateCheckPoint(int flags)
76367640
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
76377641

76387642
/*
7639-
* If this isn't a shutdown or forced checkpoint, and we have not inserted
7640-
* any XLOG records since the start of the last checkpoint, skip the
7643+
* If this isn't a shutdown or forced checkpoint, and we have not switched
7644+
* to the next WAL file since the start of the last checkpoint, skip the
76417645
* checkpoint. The idea here is to avoid inserting duplicate checkpoints
76427646
* when the system is idle. That wastes log space, and more importantly it
76437647
* exposes us to possible loss of both current and previous checkpoint
76447648
* records if the machine crashes just as we're writing the update.
76457649
* (Perhaps it'd make even more sense to checkpoint only when the previous
76467650
* checkpoint record is in a different xlog page?)
76477651
*
7648-
* We have to make two tests to determine that nothing has happened since
7649-
* the start of the last checkpoint: current insertion point must match
7650-
* the end of the last checkpoint record, and its redo pointer must point
7651-
* to itself.
7652+
* While holding the WALInsertLock we find the current WAL insertion point
7653+
* and compare that with the starting point of the last checkpoint, which
7654+
* is the redo pointer. We use the redo pointer because the start and end
7655+
* points of a checkpoint can be hundreds of files apart on large systems
7656+
* when checkpoint writes are spread out over time.
76527657
*/
76537658
if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
76547659
CHECKPOINT_FORCE)) == 0)
76557660
{
76567661
XLogRecPtr curInsert;
76577662

76587663
INSERT_RECPTR(curInsert, Insert, Insert->curridx);
7659-
if (curInsert.xlogid == ControlFile->checkPoint.xlogid &&
7660-
curInsert.xrecoff == ControlFile->checkPoint.xrecoff +
7661-
MAXALIGN(SizeOfXLogRecord + sizeof(CheckPoint)) &&
7662-
ControlFile->checkPoint.xlogid ==
7663-
ControlFile->checkPointCopy.redo.xlogid &&
7664-
ControlFile->checkPoint.xrecoff ==
7665-
ControlFile->checkPointCopy.redo.xrecoff)
7664+
XLByteToSeg(curInsert, insert_logId, insert_logSeg);
7665+
XLByteToSeg(ControlFile->checkPointCopy.redo, redo_logId, redo_logSeg);
7666+
if (insert_logId == redo_logId &&
7667+
insert_logSeg == redo_logSeg)
76667668
{
76677669
LWLockRelease(WALInsertLock);
76687670
LWLockRelease(CheckpointLock);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy