diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2024-03-05 12:09:18 +0100 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2024-03-05 12:09:18 +0100 |
commit | 1a2654b32b546d25e39a0c02410049559a3d4573 (patch) | |
tree | 00e7e463d7113fe872912f271d04671c3eb085b6 /src/backend/access/transam/subtrans.c | |
parent | 030e10ff1a365796bd4bcbbc5b6a8552f7efc765 (diff) | |
download | postgresql-1a2654b32b546d25e39a0c02410049559a3d4573.tar.gz postgresql-1a2654b32b546d25e39a0c02410049559a3d4573.zip |
Rework redundant code in subtrans.c
When this code was written the duplicity didn't matter, but with all the
SLRU-bank stuff we just added, it has become excessive. Turn it into a
simpler loop with no code duplication. Also add a test so that this
code becomes covered.
Discussion: https://postgr.es/m/202403041517.3a35jw53os65@alvherre.pgsql
Diffstat (limited to 'src/backend/access/transam/subtrans.c')
-rw-r--r-- | src/backend/access/transam/subtrans.c | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/src/backend/access/transam/subtrans.c b/src/backend/access/transam/subtrans.c index dc9566fb51b..50bb1d8cfc5 100644 --- a/src/backend/access/transam/subtrans.c +++ b/src/backend/access/transam/subtrans.c @@ -311,7 +311,7 @@ StartupSUBTRANS(TransactionId oldestActiveXID) FullTransactionId nextXid; int64 startPage; int64 endPage; - LWLock *prevlock; + LWLock *prevlock = NULL; LWLock *lock; /* @@ -324,42 +324,27 @@ StartupSUBTRANS(TransactionId oldestActiveXID) nextXid = TransamVariables->nextXid; endPage = TransactionIdToPage(XidFromFullTransactionId(nextXid)); - prevlock = SimpleLruGetBankLock(SubTransCtl, startPage); - LWLockAcquire(prevlock, LW_EXCLUSIVE); - while (startPage != endPage) + for (;;) { lock = SimpleLruGetBankLock(SubTransCtl, startPage); - - /* - * Check if we need to acquire the lock on the new bank then release - * the lock on the old bank and acquire on the new bank. - */ if (prevlock != lock) { - LWLockRelease(prevlock); + if (prevlock) + LWLockRelease(prevlock); LWLockAcquire(lock, LW_EXCLUSIVE); prevlock = lock; } (void) ZeroSUBTRANSPage(startPage); + if (startPage == endPage) + break; + startPage++; /* must account for wraparound */ if (startPage > TransactionIdToPage(MaxTransactionId)) startPage = 0; } - lock = SimpleLruGetBankLock(SubTransCtl, startPage); - - /* - * Check if we need to acquire the lock on the new bank then release the - * lock on the old bank and acquire on the new bank. - */ - if (prevlock != lock) - { - LWLockRelease(prevlock); - LWLockAcquire(lock, LW_EXCLUSIVE); - } - (void) ZeroSUBTRANSPage(startPage); LWLockRelease(lock); } |