aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/ipc
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2024-07-04 02:05:37 +0300
committerAlexander Korotkov <akorotkov@postgresql.org>2024-07-04 02:05:37 +0300
commit6897f0ec024582a570868939d3f34a6853374723 (patch)
tree8c9d63f70635cc43061f7b3e4ab8dcad9ee48ec2 /src/backend/storage/ipc
parent6c1af5482e6943a5f29b7f4ca773c720ec8202b0 (diff)
downloadpostgresql-6897f0ec024582a570868939d3f34a6853374723.tar.gz
postgresql-6897f0ec024582a570868939d3f34a6853374723.zip
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
Diffstat (limited to 'src/backend/storage/ipc')
-rw-r--r--src/backend/storage/ipc/procarray.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 9fc930e98f8..16b5803d388 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -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))
- oldestDatabaseRunningXid = xid;
+ 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;