|
7 | 7 | * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
8 | 8 | * Portions Copyright (c) 1994, Regents of the University of California
|
9 | 9 | *
|
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 $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -1331,6 +1331,40 @@ AdvanceXLInsertBuffer(bool new_segment)
|
1331 | 1331 | return update_needed;
|
1332 | 1332 | }
|
1333 | 1333 |
|
| 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 | + |
1334 | 1368 | /*
|
1335 | 1369 | * Write and/or fsync the log at least as far as WriteRqst indicates.
|
1336 | 1370 | *
|
@@ -1522,30 +1556,16 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
|
1522 | 1556 |
|
1523 | 1557 | /*
|
1524 | 1558 | * 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. |
1533 | 1563 | */
|
1534 |
| - if (IsUnderPostmaster) |
| 1564 | + if (IsUnderPostmaster && |
| 1565 | + XLogCheckpointNeeded()) |
1535 | 1566 | {
|
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()) |
1549 | 1569 | RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
|
1550 | 1570 | }
|
1551 | 1571 | }
|
|
0 commit comments