When telling the bgwriter that we need a checkpoint because too much xlog
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Oct 2007 19:39:59 +0000 (19:39 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Oct 2007 19:39:59 +0000 (19:39 +0000)
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.

src/backend/access/transam/xlog.c

index 04e180956116e201dd97edeee520b86bc504e400..8661a2fe7092d3051781e31eda3df31ad3ec4bdd 100644 (file)
@@ -1331,6 +1331,40 @@ AdvanceXLInsertBuffer(bool new_segment)
        return update_needed;
 }
 
+/*
+ * Check whether we've consumed enough xlog space that a checkpoint is needed.
+ *
+ * Caller must have just finished filling the open log file (so that
+ * openLogId/openLogSeg are valid).  We measure the distance from RedoRecPtr
+ * to the open log file and see if that exceeds CheckPointSegments.
+ *
+ * Note: it is caller's responsibility that RedoRecPtr is up-to-date.
+ */
+static bool
+XLogCheckpointNeeded(void)
+{
+       /*
+        * A straight computation of segment number could overflow 32
+        * bits.  Rather than assuming we have working 64-bit
+        * arithmetic, we compare the highest-order bits separately,
+        * and force a checkpoint immediately when they change.
+        */
+       uint32          old_segno,
+                               new_segno;
+       uint32          old_highbits,
+                               new_highbits;
+
+       old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
+               (RedoRecPtr.xrecoff / XLogSegSize);
+       old_highbits = RedoRecPtr.xlogid / XLogSegSize;
+       new_segno = (openLogId % XLogSegSize) * XLogSegsPerFile + openLogSeg;
+       new_highbits = openLogId / XLogSegSize;
+       if (new_highbits != old_highbits ||
+               new_segno >= old_segno + (uint32) (CheckPointSegments-1))
+               return true;
+       return false;
+}
+
 /*
  * Write and/or fsync the log at least as far as WriteRqst indicates.
  *
@@ -1522,30 +1556,16 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
 
                                /*
                                 * Signal bgwriter to start a checkpoint if we've consumed too
-                                * much xlog since the last one.  (We look at local copy of
-                                * RedoRecPtr which might be a little out of date, but should
-                                * be close enough for this purpose.)
-                                *
-                                * A straight computation of segment number could overflow 32
-                                * bits.  Rather than assuming we have working 64-bit
-                                * arithmetic, we compare the highest-order bits separately,
-                                * and force a checkpoint immediately when they change.
+                                * much xlog since the last one.  For speed, we first check
+                                * using the local copy of RedoRecPtr, which might be
+                                * out of date; if it looks like a checkpoint is needed,
+                                * forcibly update RedoRecPtr and recheck.
                                 */
-                               if (IsUnderPostmaster)
+                               if (IsUnderPostmaster &&
+                                       XLogCheckpointNeeded())
                                {
-                                       uint32          old_segno,
-                                                               new_segno;
-                                       uint32          old_highbits,
-                                                               new_highbits;
-
-                                       old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
-                                               (RedoRecPtr.xrecoff / XLogSegSize);
-                                       old_highbits = RedoRecPtr.xlogid / XLogSegSize;
-                                       new_segno = (openLogId % XLogSegSize) * XLogSegsPerFile +
-                                               openLogSeg;
-                                       new_highbits = openLogId / XLogSegSize;
-                                       if (new_highbits != old_highbits ||
-                                               new_segno >= old_segno + (uint32) (CheckPointSegments-1))
+                                       (void) GetRedoRecPtr();
+                                       if (XLogCheckpointNeeded())
                                                RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
                                }
                        }