1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

Optimize memory access in GetRunningTransactionData()

e85662df44 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
This commit is contained in:
Alexander Korotkov
2024-07-04 02:05:37 +03:00
parent 6c1af5482e
commit 6897f0ec02

View File

@@ -2753,8 +2753,6 @@ GetRunningTransactionData(void)
*/
for (index = 0; index < arrayP->numProcs; index++)
{
int pgprocno = arrayP->pgprocnos[index];
PGPROC *proc = &allProcs[pgprocno];
TransactionId xid;
/* Fetch xid just once - see GetNewTransactionId */
@@ -2776,11 +2774,18 @@ GetRunningTransactionData(void)
oldestRunningXid = xid;
/*
* Also, update the oldest running xid within the current database.
* Also, update the oldest running xid within the current database. As
* fetching pgprocno and PGPROC could cause cache misses, we do cheap
* TransactionId comparison first.
*/
if (proc->databaseId == MyDatabaseId &&
TransactionIdPrecedes(xid, oldestDatabaseRunningXid))
if (TransactionIdPrecedes(xid, oldestDatabaseRunningXid))
{
int pgprocno = arrayP->pgprocnos[index];
PGPROC *proc = &allProcs[pgprocno];
if (proc->databaseId == MyDatabaseId)
oldestDatabaseRunningXid = xid;
}
if (ProcGlobal->subxidStates[index].overflowed)
suboverflowed = true;