Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 06f5295

Browse files
committed
Add single-item cache when looking at topmost XID of a subtrans XID
This change affects SubTransGetTopmostTransaction(), used to find the topmost transaction ID of a given transaction ID. The cache is able to store one value, so as we can save the backend from unnecessary lookups at pg_subtrans/ on repetitive calls of this routine. There is a similar practice in transam.c, for example. Author: Simon Riggs Reviewed-by: Andrey Borodin, Julien Rouhaud Discussion: https://postgr.es/m/CANbhV-G8Co=yq4v4BkW7MJDqVt68K_8A48nAZ_+8UQS7LrwLEQ@mail.gmail.com
1 parent fbfe691 commit 06f5295

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/backend/access/transam/subtrans.c

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
#define TransactionIdToPage(xid) ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
5555
#define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
5656

57+
/*
58+
* Single-item cache for results of SubTransGetTopmostTransaction(). It's
59+
* worth having such a cache because we frequently find ourselves repeatedly
60+
* checking the same XID, for example when scanning a table just after a
61+
* bulk insert, update, or delete.
62+
*/
63+
static TransactionId cachedFetchSubXid = InvalidTransactionId;
64+
static TransactionId cachedFetchTopmostXid = InvalidTransactionId;
5765

5866
/*
5967
* Link to shared-memory data structures for SUBTRANS control
@@ -155,6 +163,13 @@ SubTransGetTopmostTransaction(TransactionId xid)
155163
/* Can't ask about stuff that might not be around anymore */
156164
Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
157165

166+
/*
167+
* Before going to the subtrans log, check our single item cache to see if
168+
* we know the result from a previous/recent request.
169+
*/
170+
if (TransactionIdEquals(xid, cachedFetchSubXid))
171+
return cachedFetchTopmostXid;
172+
158173
while (TransactionIdIsValid(parentXid))
159174
{
160175
previousXid = parentXid;
@@ -174,6 +189,9 @@ SubTransGetTopmostTransaction(TransactionId xid)
174189

175190
Assert(TransactionIdIsValid(previousXid));
176191

192+
cachedFetchSubXid = xid;
193+
cachedFetchTopmostXid = previousXid;
194+
177195
return previousXid;
178196
}
179197

0 commit comments

Comments
 (0)