Skip to content

Commit c2891b4

Browse files
committed
Initialize myProcLocks queues just once, at postmaster startup.
In assert-enabled builds, we assert during the shutdown sequence that the queues have been properly emptied, and during process startup that we are inheriting empty queues. In non-assert enabled builds, we just save a few cycles.
1 parent 391af9f commit c2891b4

File tree

1 file changed

+41
-11
lines changed
  • src/backend/storage/lmgr

1 file changed

+41
-11
lines changed

src/backend/storage/lmgr/proc.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ void
157157
InitProcGlobal(void)
158158
{
159159
PGPROC *procs;
160-
int i;
160+
int i,
161+
j;
161162
bool found;
162163
uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS;
163164

@@ -222,6 +223,10 @@ InitProcGlobal(void)
222223
procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
223224
ProcGlobal->autovacFreeProcs = &procs[i];
224225
}
226+
227+
/* Initialize myProcLocks[] shared memory queues. */
228+
for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
229+
SHMQueueInit(&(procs[i].myProcLocks[j]));
225230
}
226231

227232
/*
@@ -243,7 +248,6 @@ InitProcess(void)
243248
{
244249
/* use volatile pointer to prevent code rearrangement */
245250
volatile PROC_HDR *procglobal = ProcGlobal;
246-
int i;
247251

248252
/*
249253
* ProcGlobal should be set up already (if we are a backend, we inherit
@@ -303,8 +307,8 @@ InitProcess(void)
303307
MarkPostmasterChildActive();
304308

305309
/*
306-
* Initialize all fields of MyProc, except for the semaphore and latch,
307-
* which were prepared for us by InitProcGlobal.
310+
* Initialize all fields of MyProc, except for those previously initialized
311+
* by InitProcGlobal.
308312
*/
309313
SHMQueueElemInit(&(MyProc->links));
310314
MyProc->waitStatus = STATUS_OK;
@@ -326,8 +330,16 @@ InitProcess(void)
326330
MyProc->lwWaitLink = NULL;
327331
MyProc->waitLock = NULL;
328332
MyProc->waitProcLock = NULL;
329-
for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
330-
SHMQueueInit(&(MyProc->myProcLocks[i]));
333+
#ifdef USE_ASSERT_CHECKING
334+
if (assert_enabled)
335+
{
336+
int i;
337+
338+
/* Last process should have released all locks. */
339+
for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
340+
Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
341+
}
342+
#endif
331343
MyProc->recoveryConflictPending = false;
332344

333345
/* Initialize fields for sync rep */
@@ -408,7 +420,6 @@ InitAuxiliaryProcess(void)
408420
{
409421
PGPROC *auxproc;
410422
int proctype;
411-
int i;
412423

413424
/*
414425
* ProcGlobal should be set up already (if we are a backend, we inherit
@@ -455,8 +466,8 @@ InitAuxiliaryProcess(void)
455466
SpinLockRelease(ProcStructLock);
456467

457468
/*
458-
* Initialize all fields of MyProc, except for the semaphore and latch,
459-
* which were prepared for us by InitProcGlobal.
469+
* Initialize all fields of MyProc, except for those previously initialized
470+
* by InitProcGlobal.
460471
*/
461472
SHMQueueElemInit(&(MyProc->links));
462473
MyProc->waitStatus = STATUS_OK;
@@ -473,8 +484,16 @@ InitAuxiliaryProcess(void)
473484
MyProc->lwWaitLink = NULL;
474485
MyProc->waitLock = NULL;
475486
MyProc->waitProcLock = NULL;
476-
for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
477-
SHMQueueInit(&(MyProc->myProcLocks[i]));
487+
#ifdef USE_ASSERT_CHECKING
488+
if (assert_enabled)
489+
{
490+
int i;
491+
492+
/* Last process should have released all locks. */
493+
for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
494+
Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
495+
}
496+
#endif
478497

479498
/*
480499
* Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
@@ -687,6 +706,17 @@ ProcKill(int code, Datum arg)
687706
/* Make sure we're out of the sync rep lists */
688707
SyncRepCleanupAtProcExit();
689708

709+
#ifdef USE_ASSERT_CHECKING
710+
if (assert_enabled)
711+
{
712+
int i;
713+
714+
/* Last process should have released all locks. */
715+
for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
716+
Assert(SHMQueueEmpty(&(MyProc->myProcLocks[i])));
717+
}
718+
#endif
719+
690720
/*
691721
* Release any LW locks I am holding. There really shouldn't be any, but
692722
* it's cheap to check again before we cut the knees off the LWLock

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy