Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c8eb92

Browse files
committed
When telling the bgwriter that we need a checkpoint because too much xlog
has been consumed, recheck against the latest value of RedoRecPtr before really sending the signal. This avoids useless checkpoint activity if XLogWrite is executed when we have a very stale local copy of RedoRecPtr. The potential for useless checkpoint is very much worse in 8.3 because of the walwriter process (which never does XLogInsert), so while this behavior was intentional, it needs to be changed. Per report from Itagaki Takahiro.
1 parent 6daef2b commit 5c8eb92

File tree

1 file changed

+43
-23
lines changed
  • src/backend/access/transam

1 file changed

+43
-23
lines changed

src/backend/access/transam/xlog.c

+43-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.285 2007/09/30 17:28:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.286 2007/10/12 19:39:59 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1331,6 +1331,40 @@ AdvanceXLInsertBuffer(bool new_segment)
13311331
return update_needed;
13321332
}
13331333

1334+
/*
1335+
* Check whether we've consumed enough xlog space that a checkpoint is needed.
1336+
*
1337+
* Caller must have just finished filling the open log file (so that
1338+
* openLogId/openLogSeg are valid). We measure the distance from RedoRecPtr
1339+
* to the open log file and see if that exceeds CheckPointSegments.
1340+
*
1341+
* Note: it is caller's responsibility that RedoRecPtr is up-to-date.
1342+
*/
1343+
static bool
1344+
XLogCheckpointNeeded(void)
1345+
{
1346+
/*
1347+
* A straight computation of segment number could overflow 32
1348+
* bits. Rather than assuming we have working 64-bit
1349+
* arithmetic, we compare the highest-order bits separately,
1350+
* and force a checkpoint immediately when they change.
1351+
*/
1352+
uint32 old_segno,
1353+
new_segno;
1354+
uint32 old_highbits,
1355+
new_highbits;
1356+
1357+
old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
1358+
(RedoRecPtr.xrecoff / XLogSegSize);
1359+
old_highbits = RedoRecPtr.xlogid / XLogSegSize;
1360+
new_segno = (openLogId % XLogSegSize) * XLogSegsPerFile + openLogSeg;
1361+
new_highbits = openLogId / XLogSegSize;
1362+
if (new_highbits != old_highbits ||
1363+
new_segno >= old_segno + (uint32) (CheckPointSegments-1))
1364+
return true;
1365+
return false;
1366+
}
1367+
13341368
/*
13351369
* Write and/or fsync the log at least as far as WriteRqst indicates.
13361370
*
@@ -1522,30 +1556,16 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
15221556

15231557
/*
15241558
* Signal bgwriter to start a checkpoint if we've consumed too
1525-
* much xlog since the last one. (We look at local copy of
1526-
* RedoRecPtr which might be a little out of date, but should
1527-
* be close enough for this purpose.)
1528-
*
1529-
* A straight computation of segment number could overflow 32
1530-
* bits. Rather than assuming we have working 64-bit
1531-
* arithmetic, we compare the highest-order bits separately,
1532-
* and force a checkpoint immediately when they change.
1559+
* much xlog since the last one. For speed, we first check
1560+
* using the local copy of RedoRecPtr, which might be
1561+
* out of date; if it looks like a checkpoint is needed,
1562+
* forcibly update RedoRecPtr and recheck.
15331563
*/
1534-
if (IsUnderPostmaster)
1564+
if (IsUnderPostmaster &&
1565+
XLogCheckpointNeeded())
15351566
{
1536-
uint32 old_segno,
1537-
new_segno;
1538-
uint32 old_highbits,
1539-
new_highbits;
1540-
1541-
old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
1542-
(RedoRecPtr.xrecoff / XLogSegSize);
1543-
old_highbits = RedoRecPtr.xlogid / XLogSegSize;
1544-
new_segno = (openLogId % XLogSegSize) * XLogSegsPerFile +
1545-
openLogSeg;
1546-
new_highbits = openLogId / XLogSegSize;
1547-
if (new_highbits != old_highbits ||
1548-
new_segno >= old_segno + (uint32) (CheckPointSegments-1))
1567+
(void) GetRedoRecPtr();
1568+
if (XLogCheckpointNeeded())
15491569
RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
15501570
}
15511571
}

0 commit comments

Comments
 (0)