Skip to content

Commit d9638a3

Browse files
committed
Ensure correct minimum consistent point on standbys
Startup process has improved its calculation of incorrect minimum consistent point in 8d68ee6, which ensures that all WAL available gets replayed when doing crash recovery, and has introduced an incorrect calculation of the minimum recovery point for non-startup processes, which can cause incorrect page references on a standby when for example the background writer flushed a couple of pages on-disk but was not updating the control file to let a subsequent crash recovery replay to where it should have. The only case where this has been reported to be a problem is when a standby needs to calculate the latest removed xid when replaying a btree deletion record, so one would need connections on a standby that happen just after recovery has thought it reached a consistent point. Using a background worker which is started after the consistent point is reached would be the easiest way to get into problems if it connects to a database. Having clients which attempt to connect periodically could also be a problem, but the odds of seeing this problem are much lower. The fix used is pretty simple, as the idea is to give access to the minimum recovery point written in the control file to non-startup processes so as they use a reference, while the startup process still initializes its own references of the minimum consistent point so as the original problem with incorrect page references happening post-promotion with a crash do not show up. Reported-by: Alexander Kukushkin Diagnosed-by: Alexander Kukushkin Author: Michael Paquier Reviewed-by: Kyotaro Horiguchi, Alexander Kukushkin Discussion: https://postgr.es/m/153492341830.1368.3936905691758473953@wrigleys.postgresql.org Backpatch-through: 9.3
1 parent 7cea5e6 commit d9638a3

File tree

1 file changed

+25
-6
lines changed
  • src/backend/access/transam

1 file changed

+25
-6
lines changed

src/backend/access/transam/xlog.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,9 +2717,13 @@ UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
27172717
* i.e., we're doing crash recovery. We never modify the control file's
27182718
* value in that case, so we can short-circuit future checks here too. The
27192719
* local values of minRecoveryPoint and minRecoveryPointTLI should not be
2720-
* updated until crash recovery finishes.
2720+
* updated until crash recovery finishes. We only do this for the startup
2721+
* process as it should not update its own reference of minRecoveryPoint
2722+
* until it has finished crash recovery to make sure that all WAL
2723+
* available is replayed in this case. This also saves from extra locks
2724+
* taken on the control file from the startup process.
27212725
*/
2722-
if (XLogRecPtrIsInvalid(minRecoveryPoint))
2726+
if (XLogRecPtrIsInvalid(minRecoveryPoint) && InRecovery)
27232727
{
27242728
updateMinRecoveryPoint = false;
27252729
return;
@@ -2731,7 +2735,9 @@ UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
27312735
minRecoveryPoint = ControlFile->minRecoveryPoint;
27322736
minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
27332737

2734-
if (force || minRecoveryPoint < lsn)
2738+
if (XLogRecPtrIsInvalid(minRecoveryPoint))
2739+
updateMinRecoveryPoint = false;
2740+
else if (force || minRecoveryPoint < lsn)
27352741
{
27362742
/* use volatile pointer to prevent code rearrangement */
27372743
volatile XLogCtlData *xlogctl = XLogCtl;
@@ -3085,9 +3091,11 @@ XLogNeedsFlush(XLogRecPtr record)
30853091
* An invalid minRecoveryPoint means that we need to recover all the
30863092
* WAL, i.e., we're doing crash recovery. We never modify the control
30873093
* file's value in that case, so we can short-circuit future checks
3088-
* here too.
3094+
* here too. This triggers a quick exit path for the startup process,
3095+
* which cannot update its local copy of minRecoveryPoint as long as
3096+
* it has not replayed all WAL available when doing crash recovery.
30893097
*/
3090-
if (XLogRecPtrIsInvalid(minRecoveryPoint))
3098+
if (XLogRecPtrIsInvalid(minRecoveryPoint) && InRecovery)
30913099
updateMinRecoveryPoint = false;
30923100

30933101
/* Quick exit if already known to be updated or cannot be updated */
@@ -3104,8 +3112,19 @@ XLogNeedsFlush(XLogRecPtr record)
31043112
minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
31053113
LWLockRelease(ControlFileLock);
31063114

3115+
/*
3116+
* Check minRecoveryPoint for any other process than the startup
3117+
* process doing crash recovery, which should not update the control
3118+
* file value if crash recovery is still running.
3119+
*/
3120+
if (XLogRecPtrIsInvalid(minRecoveryPoint))
3121+
updateMinRecoveryPoint = false;
3122+
31073123
/* check again */
3108-
return record > minRecoveryPoint;
3124+
if (record <= minRecoveryPoint || !updateMinRecoveryPoint)
3125+
return false;
3126+
else
3127+
return true;
31093128
}
31103129

31113130
/* Quick exit if already known flushed */

0 commit comments

Comments
 (0)