pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed From: 盏一 <w@hidva.com>
To: Andres Freund <andres@anarazel.de>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: Re: use `proc->pgxactoff` as the value of `index` in `ProcArrayRemove()`
Date: Fri, 7 May 2021 04:36:25 +0800
Message-ID: <tencent_12E11BDD284A4A060A99EBA6@qq.com> (raw )
In-Reply-To: <20210506190841.sqxci46qamoyb55u@alap3.anarazel.de >
References: <tencent_5624AA3B116B3D1C31CA9744@qq.com >
<20210506190841.sqxci46qamoyb55u@alap3.anarazel.de >
> Sounds like a plan! Do you want to write a patch?
Add the patch.
Attachments:
0001-use-pgxactoff-as-the-value-of-index-in-ProcArrayRemo.patch (application/octet-stream, 4.3K) download
From f3176f831a110fc23c4d897f893e31400834a6a8 Mon Sep 17 00:00:00 2001
From: zhanyi <w@hidva.com>
Date: Fri, 7 May 2021 04:22:50 +0800
Subject: [PATCH] use pgxactoff as the value of index in ProcArrayRemove()
pgxactoff is always the index of pgprocno in procArray->pgprocnos.
---
src/backend/storage/ipc/procarray.c | 84 +++++++++++++++++------------
1 file changed, 49 insertions(+), 35 deletions(-)
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 5ff8cab394..d7cf4bdff0 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -511,6 +511,28 @@ ProcArrayAdd(PGPROC *proc)
LWLockRelease(ProcArrayLock);
}
+#ifdef USE_ASSERT_CHECKING
+static int
+find(int *array, int size, int val)
+{
+ for (int idx = 0; idx < size; ++idx)
+ {
+ if (array[idx] == val)
+ return idx;
+ }
+ return -1;
+}
+
+static int
+find_ex(int *array, int size, int start, int val)
+{
+ int ret = find(array + start, size - start, val);
+ if (ret >= 0)
+ ret += start;
+ return ret;
+}
+#endif
+
/*
* Remove the specified PGPROC from the shared array.
*
@@ -559,47 +581,39 @@ ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
Assert(!TransactionIdIsValid(ProcGlobal->xids[proc->pgxactoff]));
}
- Assert(TransactionIdIsValid(ProcGlobal->xids[proc->pgxactoff] == 0));
- Assert(TransactionIdIsValid(ProcGlobal->subxidStates[proc->pgxactoff].count == 0));
- Assert(TransactionIdIsValid(ProcGlobal->subxidStates[proc->pgxactoff].overflowed == false));
+ Assert(!TransactionIdIsValid(ProcGlobal->xids[proc->pgxactoff]));
+ Assert(ProcGlobal->subxidStates[proc->pgxactoff].count == 0);
+ Assert(ProcGlobal->subxidStates[proc->pgxactoff].overflowed == false);
ProcGlobal->statusFlags[proc->pgxactoff] = 0;
- for (index = 0; index < arrayP->numProcs; index++)
- {
- if (arrayP->pgprocnos[index] == proc->pgprocno)
- {
- /* Keep the PGPROC array sorted. See notes above */
- memmove(&arrayP->pgprocnos[index], &arrayP->pgprocnos[index + 1],
- (arrayP->numProcs - index - 1) * sizeof(*arrayP->pgprocnos));
- memmove(&ProcGlobal->xids[index], &ProcGlobal->xids[index + 1],
- (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->xids));
- memmove(&ProcGlobal->subxidStates[index], &ProcGlobal->subxidStates[index + 1],
- (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->subxidStates));
- memmove(&ProcGlobal->statusFlags[index], &ProcGlobal->statusFlags[index + 1],
- (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->statusFlags));
-
- arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
- arrayP->numProcs--;
-
- /* adjust for removed PGPROC */
- for (; index < arrayP->numProcs; index++)
- allProcs[arrayP->pgprocnos[index]].pgxactoff--;
+ index = proc->pgxactoff;
+ Assert(find(arrayP->pgprocnos, arrayP->numProcs, proc->pgprocno) == index);
+ Assert(find_ex(arrayP->pgprocnos, arrayP->numProcs, index + 1, proc->pgprocno) == -1);
- /*
- * Release in reversed acquisition order, to reduce frequency of
- * having to wait for XidGenLock while holding ProcArrayLock.
- */
- LWLockRelease(XidGenLock);
- LWLockRelease(ProcArrayLock);
- return;
- }
- }
+ /* Keep the PGPROC array sorted. See notes above */
+ memmove(&arrayP->pgprocnos[index], &arrayP->pgprocnos[index + 1],
+ (arrayP->numProcs - index - 1) * sizeof(*arrayP->pgprocnos));
+ memmove(&ProcGlobal->xids[index], &ProcGlobal->xids[index + 1],
+ (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->xids));
+ memmove(&ProcGlobal->subxidStates[index], &ProcGlobal->subxidStates[index + 1],
+ (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->subxidStates));
+ memmove(&ProcGlobal->statusFlags[index], &ProcGlobal->statusFlags[index + 1],
+ (arrayP->numProcs - index - 1) * sizeof(*ProcGlobal->statusFlags));
- /* Oops */
+ arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
+ arrayP->numProcs--;
+
+ /* adjust for removed PGPROC */
+ for (; index < arrayP->numProcs; index++)
+ allProcs[arrayP->pgprocnos[index]].pgxactoff--;
+
+ /*
+ * Release in reversed acquisition order, to reduce frequency of
+ * having to wait for XidGenLock while holding ProcArrayLock.
+ */
LWLockRelease(XidGenLock);
LWLockRelease(ProcArrayLock);
-
- elog(LOG, "failed to find proc %p in ProcArray", proc);
+ return;
}
--
2.26.2
view thread (8+ messages) latest in thread
Message-ID: <tencent_12E11BDD284A4A060A99EBA6@qq.com>
Permalink: ../tencent_12E11BDD284A4A060A99EBA6@qq.com/
Also on: postgresql.org/message-id/tencent_12E11BDD284A4A060A99EBA6@qq.com
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: w@hidva.com, andres@anarazel.de
Subject: Re: use `proc->pgxactoff` as the value of `index` in `ProcArrayRemove()`
In-Reply-To: <tencent_12E11BDD284A4A060A99EBA6@qq.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox