Skip to content

Commit a751714

Browse files
committed
Fix race between GetNewTransactionId and GetOldestActiveTransactionId.
The race condition goes like this: 1. GetNewTransactionId advances nextXid e.g. from 100 to 101 2. GetOldestActiveTransactionId reads the new nextXid, 101 3. GetOldestActiveTransactionId loops through the proc array. There are no active XIDs there, so it returns 101 as the oldest active XID. 4. GetNewTransactionid stores XID 100 to MyPgXact->xid So, GetOldestActiveTransactionId returned XID 101, even though 100 only just started and is surely still running. This would be hard to hit in practice, and even harder to spot any ill effect if it happens. GetOldestActiveTransactionId is only used when creating a checkpoint in a master server, and the race condition can only happen on an online checkpoint, as there are no backends running during a shutdown checkpoint. The oldestActiveXid value of an online checkpoint is only used when starting up a hot standby server, to determine the starting point where pg_subtrans is initialized from. For the race condition to happen, there must be no other XIDs in the proc array that would hold back the oldest-active XID value, which means that the missed XID must be a top transaction's XID. However, pg_subtrans is not used for top XIDs, so I believe an off-by-one error is in fact inconsequential. Nevertheless, let's fix it, as it's clearly wrong and the fix is simple. This has been wrong ever since hot standby was introduced, so backport to all supported versions. Discussion: https://www.postgresql.org/message-id/e7258662-82b6-7a45-56d4-99b337a32bf7@iki.fi
1 parent a2320b3 commit a751714

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,20 +1878,21 @@ GetOldestActiveTransactionId(void)
18781878

18791879
Assert(!RecoveryInProgress());
18801880

1881-
LWLockAcquire(ProcArrayLock, LW_SHARED);
1882-
18831881
/*
1884-
* It's okay to read nextXid without acquiring XidGenLock because (1) we
1885-
* assume TransactionIds can be read atomically and (2) we don't care if
1886-
* we get a slightly stale value. It can't be very stale anyway, because
1887-
* the LWLockAcquire above will have done any necessary memory
1888-
* interlocking.
1882+
* Read nextXid, as the upper bound of what's still active.
1883+
*
1884+
* Reading a TransactionId is atomic, but we must grab the lock to make
1885+
* sure that all XIDs < nextXid are already present in the proc array (or
1886+
* have already completed), when we spin over it.
18891887
*/
1888+
LWLockAcquire(XidGenLock, LW_SHARED);
18901889
oldestRunningXid = ShmemVariableCache->nextXid;
1890+
LWLockRelease(XidGenLock);
18911891

18921892
/*
18931893
* Spin over procArray collecting all xids and subxids.
18941894
*/
1895+
LWLockAcquire(ProcArrayLock, LW_SHARED);
18951896
for (index = 0; index < arrayP->numProcs; index++)
18961897
{
18971898
int pgprocno = arrayP->pgprocnos[index];
@@ -1913,7 +1914,6 @@ GetOldestActiveTransactionId(void)
19131914
* smaller than oldestRunningXid
19141915
*/
19151916
}
1916-
19171917
LWLockRelease(ProcArrayLock);
19181918

19191919
return oldestRunningXid;

0 commit comments

Comments
 (0)