Skip to content

Commit 28f3915

Browse files
committed
Remove superfluous 'pgprocno' field from PGPROC
It was always just the index of the PGPROC entry from the beginning of the proc array. Introduce a macro to compute it from the pointer instead. Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/8171f1aa-496f-46a6-afc3-c46fe7a9b407@iki.fi
1 parent 4989ce7 commit 28f3915

File tree

14 files changed

+46
-43
lines changed

14 files changed

+46
-43
lines changed

src/backend/access/transam/clog.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status,
425425
{
426426
volatile PROC_HDR *procglobal = ProcGlobal;
427427
PGPROC *proc = MyProc;
428+
int pgprocno = MyProcNumber;
428429
uint32 nextidx;
429430
uint32 wakeidx;
430431

@@ -458,7 +459,7 @@ TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status,
458459
* less efficiently.
459460
*/
460461
if (nextidx != INVALID_PGPROCNO &&
461-
ProcGlobal->allProcs[nextidx].clogGroupMemberPage != proc->clogGroupMemberPage)
462+
GetPGProcByNumber(nextidx)->clogGroupMemberPage != proc->clogGroupMemberPage)
462463
{
463464
/*
464465
* Ensure that this proc is not a member of any clog group that
@@ -473,7 +474,7 @@ TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status,
473474

474475
if (pg_atomic_compare_exchange_u32(&procglobal->clogGroupFirst,
475476
&nextidx,
476-
(uint32) proc->pgprocno))
477+
(uint32) pgprocno))
477478
break;
478479
}
479480

src/backend/access/transam/twophase.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ TwoPhaseShmemInit(void)
284284
TwoPhaseState->freeGXacts = &gxacts[i];
285285

286286
/* associate it with a PGPROC assigned by InitProcGlobal */
287-
gxacts[i].pgprocno = PreparedXactProcs[i].pgprocno;
287+
gxacts[i].pgprocno = GetNumberFromPGProc(&PreparedXactProcs[i]);
288288

289289
/*
290290
* Assign a unique ID for each dummy proc, so that the range of
@@ -461,7 +461,6 @@ MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid, const char *gid,
461461

462462
/* Initialize the PGPROC entry */
463463
MemSet(proc, 0, sizeof(PGPROC));
464-
proc->pgprocno = gxact->pgprocno;
465464
dlist_node_init(&proc->links);
466465
proc->waitStatus = PROC_WAIT_STATUS_OK;
467466
if (LocalTransactionIdIsValid(MyProc->lxid))
@@ -780,7 +779,7 @@ pg_prepared_xact(PG_FUNCTION_ARGS)
780779
while (status->array != NULL && status->currIdx < status->ngxacts)
781780
{
782781
GlobalTransaction gxact = &status->array[status->currIdx++];
783-
PGPROC *proc = &ProcGlobal->allProcs[gxact->pgprocno];
782+
PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
784783
Datum values[5] = {0};
785784
bool nulls[5] = {0};
786785
HeapTuple tuple;
@@ -935,7 +934,7 @@ TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
935934
{
936935
GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
937936

938-
return &ProcGlobal->allProcs[gxact->pgprocno];
937+
return GetPGProcByNumber(gxact->pgprocno);
939938
}
940939

941940
/************************************************************************/
@@ -1080,7 +1079,7 @@ save_state_data(const void *data, uint32 len)
10801079
void
10811080
StartPrepare(GlobalTransaction gxact)
10821081
{
1083-
PGPROC *proc = &ProcGlobal->allProcs[gxact->pgprocno];
1082+
PGPROC *proc = GetPGProcByNumber(gxact->pgprocno);
10841083
TransactionId xid = gxact->xid;
10851084
TwoPhaseFileHeader hdr;
10861085
TransactionId *children;
@@ -1539,7 +1538,7 @@ FinishPreparedTransaction(const char *gid, bool isCommit)
15391538
* try to commit the same GID at once.
15401539
*/
15411540
gxact = LockGXact(gid, GetUserId());
1542-
proc = &ProcGlobal->allProcs[gxact->pgprocno];
1541+
proc = GetPGProcByNumber(gxact->pgprocno);
15431542
xid = gxact->xid;
15441543

15451544
/*

src/backend/access/transam/xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ WALInsertLockAcquire(void)
13791379
static int lockToTry = -1;
13801380

13811381
if (lockToTry == -1)
1382-
lockToTry = MyProc->pgprocno % NUM_XLOGINSERT_LOCKS;
1382+
lockToTry = MyProcNumber % NUM_XLOGINSERT_LOCKS;
13831383
MyLockNo = lockToTry;
13841384

13851385
/*

src/backend/postmaster/bgwriter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ BackgroundWriterMain(void)
326326
if (rc == WL_TIMEOUT && can_hibernate && prev_hibernate)
327327
{
328328
/* Ask for notification at next buffer allocation */
329-
StrategyNotifyBgWriter(MyProc->pgprocno);
329+
StrategyNotifyBgWriter(MyProcNumber);
330330
/* Sleep ... */
331331
(void) WaitLatch(MyLatch,
332332
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,

src/backend/postmaster/pgarch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ PgArchiverMain(void)
242242
* Advertise our pgprocno so that backends can use our latch to wake us up
243243
* while we're sleeping.
244244
*/
245-
PgArch->pgprocno = MyProc->pgprocno;
245+
PgArch->pgprocno = MyProcNumber;
246246

247247
/* Create workspace for pgarch_readyXlog() */
248248
arch_files = palloc(sizeof(struct arch_files_state));

src/backend/postmaster/walsummarizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ WalSummarizerMain(void)
248248
/* Advertise ourselves. */
249249
on_shmem_exit(WalSummarizerShutdown, (Datum) 0);
250250
LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
251-
WalSummarizerCtl->summarizer_pgprocno = MyProc->pgprocno;
251+
WalSummarizerCtl->summarizer_pgprocno = MyProcNumber;
252252
LWLockRelease(WALSummarizerLock);
253253

254254
/* Create and switch to a memory context that we can reset on error. */

src/backend/storage/buffer/bufmgr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,7 +4780,7 @@ UnlockBuffers(void)
47804780
* got a cancel/die interrupt before getting the signal.
47814781
*/
47824782
if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
4783-
buf->wait_backend_pgprocno == MyProc->pgprocno)
4783+
buf->wait_backend_pgprocno == MyProcNumber)
47844784
buf_state &= ~BM_PIN_COUNT_WAITER;
47854785

47864786
UnlockBufHdr(buf, buf_state);
@@ -4930,7 +4930,7 @@ LockBufferForCleanup(Buffer buffer)
49304930
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
49314931
elog(ERROR, "multiple backends attempting to wait for pincount 1");
49324932
}
4933-
bufHdr->wait_backend_pgprocno = MyProc->pgprocno;
4933+
bufHdr->wait_backend_pgprocno = MyProcNumber;
49344934
PinCountWaitBuf = bufHdr;
49354935
buf_state |= BM_PIN_COUNT_WAITER;
49364936
UnlockBufHdr(bufHdr, buf_state);
@@ -4994,7 +4994,7 @@ LockBufferForCleanup(Buffer buffer)
49944994
*/
49954995
buf_state = LockBufHdr(bufHdr);
49964996
if ((buf_state & BM_PIN_COUNT_WAITER) != 0 &&
4997-
bufHdr->wait_backend_pgprocno == MyProc->pgprocno)
4997+
bufHdr->wait_backend_pgprocno == MyProcNumber)
49984998
buf_state &= ~BM_PIN_COUNT_WAITER;
49994999
UnlockBufHdr(bufHdr, buf_state);
50005000

src/backend/storage/ipc/procarray.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ CreateSharedProcArray(void)
468468
void
469469
ProcArrayAdd(PGPROC *proc)
470470
{
471+
int pgprocno = GetNumberFromPGProc(proc);
471472
ProcArrayStruct *arrayP = procArray;
472473
int index;
473474
int movecount;
@@ -499,13 +500,13 @@ ProcArrayAdd(PGPROC *proc)
499500
*/
500501
for (index = 0; index < arrayP->numProcs; index++)
501502
{
502-
int procno PG_USED_FOR_ASSERTS_ONLY = arrayP->pgprocnos[index];
503+
int this_procno = arrayP->pgprocnos[index];
503504

504-
Assert(procno >= 0 && procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
505-
Assert(allProcs[procno].pgxactoff == index);
505+
Assert(this_procno >= 0 && this_procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
506+
Assert(allProcs[this_procno].pgxactoff == index);
506507

507508
/* If we have found our right position in the array, break */
508-
if (arrayP->pgprocnos[index] > proc->pgprocno)
509+
if (this_procno > pgprocno)
509510
break;
510511
}
511512

@@ -523,7 +524,7 @@ ProcArrayAdd(PGPROC *proc)
523524
&ProcGlobal->statusFlags[index],
524525
movecount * sizeof(*ProcGlobal->statusFlags));
525526

526-
arrayP->pgprocnos[index] = proc->pgprocno;
527+
arrayP->pgprocnos[index] = GetNumberFromPGProc(proc);
527528
proc->pgxactoff = index;
528529
ProcGlobal->xids[index] = proc->xid;
529530
ProcGlobal->subxidStates[index] = proc->subxidStatus;
@@ -791,6 +792,7 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
791792
static void
792793
ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
793794
{
795+
int pgprocno = GetNumberFromPGProc(proc);
794796
PROC_HDR *procglobal = ProcGlobal;
795797
uint32 nextidx;
796798
uint32 wakeidx;
@@ -808,7 +810,7 @@ ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
808810

809811
if (pg_atomic_compare_exchange_u32(&procglobal->procArrayGroupFirst,
810812
&nextidx,
811-
(uint32) proc->pgprocno))
813+
(uint32) pgprocno))
812814
break;
813815
}
814816

src/backend/storage/lmgr/condition_variable.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ConditionVariableInit(ConditionVariable *cv)
5757
void
5858
ConditionVariablePrepareToSleep(ConditionVariable *cv)
5959
{
60-
int pgprocno = MyProc->pgprocno;
60+
int pgprocno = MyProcNumber;
6161

6262
/*
6363
* If some other sleep is already prepared, cancel it; this is necessary
@@ -181,10 +181,10 @@ ConditionVariableTimedSleep(ConditionVariable *cv, long timeout,
181181
* guarantee not to return spuriously, we'll avoid this obvious case.
182182
*/
183183
SpinLockAcquire(&cv->mutex);
184-
if (!proclist_contains(&cv->wakeup, MyProc->pgprocno, cvWaitLink))
184+
if (!proclist_contains(&cv->wakeup, MyProcNumber, cvWaitLink))
185185
{
186186
done = true;
187-
proclist_push_tail(&cv->wakeup, MyProc->pgprocno, cvWaitLink);
187+
proclist_push_tail(&cv->wakeup, MyProcNumber, cvWaitLink);
188188
}
189189
SpinLockRelease(&cv->mutex);
190190

@@ -236,8 +236,8 @@ ConditionVariableCancelSleep(void)
236236
return false;
237237

238238
SpinLockAcquire(&cv->mutex);
239-
if (proclist_contains(&cv->wakeup, MyProc->pgprocno, cvWaitLink))
240-
proclist_delete(&cv->wakeup, MyProc->pgprocno, cvWaitLink);
239+
if (proclist_contains(&cv->wakeup, MyProcNumber, cvWaitLink))
240+
proclist_delete(&cv->wakeup, MyProcNumber, cvWaitLink);
241241
else
242242
signaled = true;
243243
SpinLockRelease(&cv->mutex);
@@ -281,7 +281,7 @@ ConditionVariableSignal(ConditionVariable *cv)
281281
void
282282
ConditionVariableBroadcast(ConditionVariable *cv)
283283
{
284-
int pgprocno = MyProc->pgprocno;
284+
int pgprocno = MyProcNumber;
285285
PGPROC *proc = NULL;
286286
bool have_sentinel = false;
287287

src/backend/storage/lmgr/lwlock.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,9 @@ LWLockQueueSelf(LWLock *lock, LWLockMode mode)
10561056

10571057
/* LW_WAIT_UNTIL_FREE waiters are always at the front of the queue */
10581058
if (mode == LW_WAIT_UNTIL_FREE)
1059-
proclist_push_head(&lock->waiters, MyProc->pgprocno, lwWaitLink);
1059+
proclist_push_head(&lock->waiters, MyProcNumber, lwWaitLink);
10601060
else
1061-
proclist_push_tail(&lock->waiters, MyProc->pgprocno, lwWaitLink);
1061+
proclist_push_tail(&lock->waiters, MyProcNumber, lwWaitLink);
10621062

10631063
/* Can release the mutex now */
10641064
LWLockWaitListUnlock(lock);
@@ -1097,7 +1097,7 @@ LWLockDequeueSelf(LWLock *lock)
10971097
*/
10981098
on_waitlist = MyProc->lwWaiting == LW_WS_WAITING;
10991099
if (on_waitlist)
1100-
proclist_delete(&lock->waiters, MyProc->pgprocno, lwWaitLink);
1100+
proclist_delete(&lock->waiters, MyProcNumber, lwWaitLink);
11011101

11021102
if (proclist_is_empty(&lock->waiters) &&
11031103
(pg_atomic_read_u32(&lock->state) & LW_FLAG_HAS_WAITERS) != 0)

src/backend/storage/lmgr/predicate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ GetSerializableTransactionSnapshotInt(Snapshot snapshot,
18301830
sxact->finishedBefore = InvalidTransactionId;
18311831
sxact->xmin = snapshot->xmin;
18321832
sxact->pid = MyProcPid;
1833-
sxact->pgprocno = MyProc->pgprocno;
1833+
sxact->pgprocno = MyProcNumber;
18341834
dlist_init(&sxact->predicateLocks);
18351835
dlist_node_init(&sxact->finishedLink);
18361836
sxact->flags = 0;

src/backend/storage/lmgr/proc.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ bool log_lock_waits = false;
6565

6666
/* Pointer to this process's PGPROC struct, if any */
6767
PGPROC *MyProc = NULL;
68+
int MyProcNumber = INVALID_PGPROCNO;
6869

6970
/*
7071
* This spinlock protects the freelist of recycled PGPROC structures.
@@ -228,7 +229,6 @@ InitProcGlobal(void)
228229
InitSharedLatch(&(proc->procLatch));
229230
LWLockInitialize(&(proc->fpInfoLock), LWTRANCHE_LOCK_FASTPATH);
230231
}
231-
proc->pgprocno = i;
232232

233233
/*
234234
* Newly created PGPROCs for normal backends, autovacuum and bgworkers
@@ -353,6 +353,7 @@ InitProcess(void)
353353
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
354354
errmsg("sorry, too many clients already")));
355355
}
356+
MyProcNumber = GetNumberFromPGProc(MyProc);
356357

357358
/*
358359
* Cross-check that the PGPROC is of the type we expect; if this were not
@@ -566,6 +567,8 @@ InitAuxiliaryProcess(void)
566567

567568
SpinLockRelease(ProcStructLock);
568569

570+
MyProcNumber = GetNumberFromPGProc(MyProc);
571+
569572
/*
570573
* Initialize all fields of MyProc, except for those previously
571574
* initialized by InitProcGlobal.
@@ -907,6 +910,7 @@ ProcKill(int code, Datum arg)
907910

908911
proc = MyProc;
909912
MyProc = NULL;
913+
MyProcNumber = INVALID_PGPROCNO;
910914
DisownLatch(&proc->procLatch);
911915

912916
procgloballist = proc->procgloballist;
@@ -978,6 +982,7 @@ AuxiliaryProcKill(int code, Datum arg)
978982

979983
proc = MyProc;
980984
MyProc = NULL;
985+
MyProcNumber = INVALID_PGPROCNO;
981986
DisownLatch(&proc->procLatch);
982987

983988
SpinLockAcquire(ProcStructLock);
@@ -1903,10 +1908,9 @@ BecomeLockGroupMember(PGPROC *leader, int pid)
19031908

19041909
/*
19051910
* Get lock protecting the group fields. Note LockHashPartitionLockByProc
1906-
* accesses leader->pgprocno in a PGPROC that might be free. This is safe
1907-
* because all PGPROCs' pgprocno fields are set during shared memory
1908-
* initialization and never change thereafter; so we will acquire the
1909-
* correct lock even if the leader PGPROC is in process of being recycled.
1911+
* calculates the proc number based on the PGPROC slot without looking at
1912+
* its contents, so we will acquire the correct lock even if the leader
1913+
* PGPROC is in process of being recycled.
19101914
*/
19111915
leader_lwlock = LockHashPartitionLockByProc(leader);
19121916
LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);

src/include/storage/lock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ typedef enum
540540
* used for a given lock group is determined by the group leader's pgprocno.
541541
*/
542542
#define LockHashPartitionLockByProc(leader_pgproc) \
543-
LockHashPartitionLock((leader_pgproc)->pgprocno)
543+
LockHashPartitionLock(GetNumberFromPGProc(leader_pgproc))
544544

545545
/*
546546
* function prototypes

src/include/storage/proc.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,6 @@ struct PGPROC
194194
int pgxactoff; /* offset into various ProcGlobal->arrays with
195195
* data mirrored from this PGPROC */
196196

197-
int pgprocno; /* Number of this PGPROC in
198-
* ProcGlobal->allProcs array. This is set
199-
* once by InitProcGlobal().
200-
* ProcGlobal->allProcs[n].pgprocno == n */
201-
202197
/* These fields are zero while a backend is still starting up: */
203198
BackendId backendId; /* This backend's backend ID (if assigned) */
204199
Oid databaseId; /* OID of database this backend is using */
@@ -307,6 +302,7 @@ struct PGPROC
307302

308303

309304
extern PGDLLIMPORT PGPROC *MyProc;
305+
extern PGDLLIMPORT int MyProcNumber; /* same as GetNumberFromPGProc(MyProc) */
310306

311307
/*
312308
* There is one ProcGlobal struct for the whole database cluster.
@@ -410,8 +406,9 @@ extern PGDLLIMPORT PROC_HDR *ProcGlobal;
410406

411407
extern PGDLLIMPORT PGPROC *PreparedXactProcs;
412408

413-
/* Accessor for PGPROC given a pgprocno. */
409+
/* Accessor for PGPROC given a pgprocno, and vice versa. */
414410
#define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
411+
#define GetNumberFromPGProc(proc) ((proc) - &ProcGlobal->allProcs[0])
415412

416413
/*
417414
* We set aside some extra PGPROC structures for auxiliary processes,

0 commit comments

Comments
 (0)