postgres.git / summary / log / commit / refs

commit    40c49bd7198e262b570e5f40c7e2bbb3b8ced609
Author:   Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date:     Thu Jul 13 12:47:02 2017 +0000
First released in: 10.0, 9.2.22, 9.3.18, 9.4.13, 9.5.8, 9.6.4

    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


src/backend/storage/ipc/procarray.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 2f22937e693..ef97101141c 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -1764,20 +1764,21 @@ GetOldestActiveTransactionId(void) Assert(!RecoveryInProgress()); - 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. + * Read nextXid, as the upper bound of what's still active. + * + * Reading a TransactionId is atomic, but we must grab the lock to make + * sure that all XIDs < nextXid are already present in the proc array (or + * have already completed), when we spin over it. */ + LWLockAcquire(XidGenLock, LW_SHARED); oldestRunningXid = ShmemVariableCache->nextXid; + LWLockRelease(XidGenLock); /* * Spin over procArray collecting all xids and subxids. */ + LWLockAcquire(ProcArrayLock, LW_SHARED); for (index = 0; index < arrayP->numProcs; index++) { int pgprocno = arrayP->pgprocnos[index]; @@ -1799,7 +1800,6 @@ GetOldestActiveTransactionId(void) * smaller than oldestRunningXid */ } - LWLockRelease(ProcArrayLock); return oldestRunningXid; [parent: 75670ec37362]