mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Add locking around WAL-replay modification of shared-memory variables.
Originally, most of this code assumed that no Postgres backends could be running concurrently with it, and so no locking could be needed. That assumption fails in Hot Standby. While it's still true that Hot Standby backends should never change values like nextXid, they can examine them, and consistency is important in some cases such as when computing a snapshot. Therefore, prudence requires that WAL replay code obtain the relevant locks when modifying such variables, even though it can examine them without taking a lock. We were following that coding rule in some places but not all. This commit applies the coding rule uniformly to all updates of ShmemVariableCache and MultiXactState fields; a search of the replay routines did not find any other cases that seemed to be at risk. In addition, this commit fixes a longstanding thinko in replay of NEXTOID and checkpoint records: we tried to advance nextOid only if it was behind the value in the WAL record, but the comparison would draw the wrong conclusion if OID wraparound had occurred since the previous value. Better to just unconditionally assign the new value, since OID assignment shouldn't be happening during replay anyway. The additional locking seems to be more in the nature of future-proofing than fixing any live bug, so I am not going to back-patch it. The NEXTOID fix will be back-patched separately.
This commit is contained in:
@ -654,17 +654,28 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
|
||||
running->latestCompletedXid))
|
||||
ShmemVariableCache->latestCompletedXid = running->latestCompletedXid;
|
||||
|
||||
/* nextXid must be beyond any observed xid */
|
||||
Assert(TransactionIdIsNormal(ShmemVariableCache->latestCompletedXid));
|
||||
|
||||
LWLockRelease(ProcArrayLock);
|
||||
|
||||
/*
|
||||
* ShmemVariableCache->nextXid must be beyond any observed xid.
|
||||
*
|
||||
* We don't expect anyone else to modify nextXid, hence we don't need to
|
||||
* hold a lock while examining it. We still acquire the lock to modify
|
||||
* it, though.
|
||||
*/
|
||||
nextXid = latestObservedXid;
|
||||
TransactionIdAdvance(nextXid);
|
||||
if (TransactionIdFollows(nextXid, ShmemVariableCache->nextXid))
|
||||
{
|
||||
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
|
||||
ShmemVariableCache->nextXid = nextXid;
|
||||
LWLockRelease(XidGenLock);
|
||||
}
|
||||
|
||||
Assert(TransactionIdIsNormal(ShmemVariableCache->latestCompletedXid));
|
||||
Assert(TransactionIdIsValid(ShmemVariableCache->nextXid));
|
||||
|
||||
LWLockRelease(ProcArrayLock);
|
||||
|
||||
KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
|
||||
if (standbyState == STANDBY_SNAPSHOT_READY)
|
||||
elog(trace_recovery(DEBUG1), "recovery snapshots are now enabled");
|
||||
@ -1690,6 +1701,13 @@ GetOldestActiveTransactionId(void)
|
||||
|
||||
LWLockAcquire(ProcArrayLock, LW_SHARED);
|
||||
|
||||
/*
|
||||
* It's okay to read nextXid without acquiring XidGenLock because (1) we
|
||||
* assume TransactionIds can be read atomically and (2) we don't care if
|
||||
* we get a slightly stale value. It can't be very stale anyway, because
|
||||
* the LWLockAcquire above will have done any necessary memory
|
||||
* interlocking.
|
||||
*/
|
||||
oldestRunningXid = ShmemVariableCache->nextXid;
|
||||
|
||||
/*
|
||||
@ -2609,7 +2627,9 @@ RecordKnownAssignedTransactionIds(TransactionId xid)
|
||||
/* ShmemVariableCache->nextXid must be beyond any observed xid */
|
||||
next_expected_xid = latestObservedXid;
|
||||
TransactionIdAdvance(next_expected_xid);
|
||||
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
|
||||
ShmemVariableCache->nextXid = next_expected_xid;
|
||||
LWLockRelease(XidGenLock);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user