Skip to content

Commit 6897f0e

Browse files
committed
Optimize memory access in GetRunningTransactionData()
e85662d made GetRunningTransactionData() calculate the oldest running transaction id within the current database. This commit optimized this calculation by performing a cheap transaction id comparison before fetching the process database id, while the latter could cause extra cache misses. Reported-by: Noah Misch Discussion: https://postgr.es/m/20240630231816.bf.nmisch%40google.com
1 parent 6c1af54 commit 6897f0e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,8 +2753,6 @@ GetRunningTransactionData(void)
27532753
*/
27542754
for (index = 0; index < arrayP->numProcs; index++)
27552755
{
2756-
int pgprocno = arrayP->pgprocnos[index];
2757-
PGPROC *proc = &allProcs[pgprocno];
27582756
TransactionId xid;
27592757

27602758
/* Fetch xid just once - see GetNewTransactionId */
@@ -2776,11 +2774,18 @@ GetRunningTransactionData(void)
27762774
oldestRunningXid = xid;
27772775

27782776
/*
2779-
* Also, update the oldest running xid within the current database.
2777+
* Also, update the oldest running xid within the current database. As
2778+
* fetching pgprocno and PGPROC could cause cache misses, we do cheap
2779+
* TransactionId comparison first.
27802780
*/
2781-
if (proc->databaseId == MyDatabaseId &&
2782-
TransactionIdPrecedes(xid, oldestDatabaseRunningXid))
2783-
oldestDatabaseRunningXid = xid;
2781+
if (TransactionIdPrecedes(xid, oldestDatabaseRunningXid))
2782+
{
2783+
int pgprocno = arrayP->pgprocnos[index];
2784+
PGPROC *proc = &allProcs[pgprocno];
2785+
2786+
if (proc->databaseId == MyDatabaseId)
2787+
oldestDatabaseRunningXid = xid;
2788+
}
27842789

27852790
if (ProcGlobal->subxidStates[index].overflowed)
27862791
suboverflowed = true;

0 commit comments

Comments
 (0)