From 5ea90bad6f236dc38ab26f025ee03a3caa9a5802 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Fri, 1 Aug 2014 16:02:12 -0400 Subject: [PATCH] Track in the PGPROC the ProcGlobal list from which it originated. This enables us to ensure that a PGPROC is also put back on the same list from which it was taken, and is necessary infrastructure for allowing one process to release a PGPROC belonging to another process. --- src/backend/storage/lmgr/proc.c | 55 ++++++++++++++++----------------- src/include/storage/proc.h | 1 + 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index ea88a24144..de7608ac80 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -240,18 +240,21 @@ InitProcGlobal(void) /* PGPROC for normal backend, add to freeProcs list */ procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs; ProcGlobal->freeProcs = &procs[i]; + procs[i].procgloballist = &ProcGlobal->freeProcs; } else if (i < MaxConnections + autovacuum_max_workers + 1) { /* PGPROC for AV launcher/worker, add to autovacFreeProcs list */ procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs; ProcGlobal->autovacFreeProcs = &procs[i]; + procs[i].procgloballist = &ProcGlobal->autovacFreeProcs; } else if (i < MaxBackends) { /* PGPROC for bgworker, add to bgworkerFreeProcs list */ procs[i].links.next = (SHM_QUEUE *) ProcGlobal->bgworkerFreeProcs; ProcGlobal->bgworkerFreeProcs = &procs[i]; + procs[i].procgloballist = &ProcGlobal->bgworkerFreeProcs; } /* Initialize myProcLocks[] shared memory queues. */ @@ -279,6 +282,7 @@ InitProcess(void) { /* use volatile pointer to prevent code rearrangement */ volatile PROC_HDR *procglobal = ProcGlobal; + PGPROC * volatile * procgloballist; /* * ProcGlobal should be set up already (if we are a backend, we inherit @@ -297,9 +301,17 @@ InitProcess(void) */ InitializeLatchSupport(); + /* Decide which list should supply our PGPROC. */ + if (IsAnyAutoVacuumProcess()) + procgloballist = &procglobal->autovacFreeProcs; + else if (IsBackgroundWorker) + procgloballist = &procglobal->bgworkerFreeProcs; + else + procgloballist = &procglobal->freeProcs; + /* - * Try to get a proc struct from the free list. If this fails, we must be - * out of PGPROC structures (not to mention semaphores). + * Try to get a proc struct from the appropriate free list. If this + * fails, we must be out of PGPROC structures (not to mention semaphores). * * While we are holding the ProcStructLock, also copy the current shared * estimate of spins_per_delay to local storage. @@ -308,21 +320,11 @@ InitProcess(void) set_spins_per_delay(procglobal->spins_per_delay); - if (IsAnyAutoVacuumProcess()) - MyProc = procglobal->autovacFreeProcs; - else if (IsBackgroundWorker) - MyProc = procglobal->bgworkerFreeProcs; - else - MyProc = procglobal->freeProcs; + MyProc = *procgloballist; if (MyProc != NULL) { - if (IsAnyAutoVacuumProcess()) - procglobal->autovacFreeProcs = (PGPROC *) MyProc->links.next; - else if (IsBackgroundWorker) - procglobal->bgworkerFreeProcs = (PGPROC *) MyProc->links.next; - else - procglobal->freeProcs = (PGPROC *) MyProc->links.next; + *procgloballist = (PGPROC *) MyProc->links.next; SpinLockRelease(ProcStructLock); } else @@ -340,6 +342,12 @@ InitProcess(void) } MyPgXact = &ProcGlobal->allPgXact[MyProc->pgprocno]; + /* + * Cross-check that the PGPROC is of the type we expect; if this were + * not the case, it would get returned to the wrong list. + */ + Assert(MyProc->procgloballist == procgloballist); + /* * Now that we have a PGPROC, mark ourselves as an active postmaster * child; this is so that the postmaster can detect it if we exit without @@ -774,6 +782,7 @@ ProcKill(int code, Datum arg) /* use volatile pointer to prevent code rearrangement */ volatile PROC_HDR *procglobal = ProcGlobal; PGPROC *proc; + PGPROC * volatile * procgloballist; Assert(MyProc != NULL); @@ -810,24 +819,12 @@ ProcKill(int code, Datum arg) MyProc = NULL; DisownLatch(&proc->procLatch); + procgloballist = proc->procgloballist; SpinLockAcquire(ProcStructLock); /* Return PGPROC structure (and semaphore) to appropriate freelist */ - if (IsAnyAutoVacuumProcess()) - { - proc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs; - procglobal->autovacFreeProcs = proc; - } - else if (IsBackgroundWorker) - { - proc->links.next = (SHM_QUEUE *) procglobal->bgworkerFreeProcs; - procglobal->bgworkerFreeProcs = proc; - } - else - { - proc->links.next = (SHM_QUEUE *) procglobal->freeProcs; - procglobal->freeProcs = proc; - } + proc->links.next = (SHM_QUEUE *) *procgloballist; + *procgloballist = proc; /* Update shared estimate of spins_per_delay */ procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay); diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index c23f4da5b6..f2fec32877 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -77,6 +77,7 @@ struct PGPROC { /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */ SHM_QUEUE links; /* list link if process is in a list */ + PGPROC **procgloballist; /* procglobal list that owns this PGPROC */ PGSemaphoreData sem; /* ONE semaphore to sleep on */ int waitStatus; /* STATUS_WAITING, STATUS_OK or STATUS_ERROR */ -- 2.39.5