summaryrefslogtreecommitdiff
path: root/src/backend/storage/lmgr/proc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/storage/lmgr/proc.c')
-rw-r--r--src/backend/storage/lmgr/proc.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 9ef008e7331..58d95bc0d6b 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -187,7 +187,8 @@ InitProcGlobal(void)
ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
/*
- * Pre-create the PGPROC structures and create a semaphore for each.
+ * Pre-create the PGPROC structures and create a semaphore and latch
+ * for each.
*/
procs = (PGPROC *) ShmemAlloc((MaxConnections) * sizeof(PGPROC));
if (!procs)
@@ -198,9 +199,9 @@ InitProcGlobal(void)
for (i = 0; i < MaxConnections; i++)
{
PGSemaphoreCreate(&(procs[i].sem));
+ InitSharedLatch(&procs[i].procLatch);
procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
ProcGlobal->freeProcs = &procs[i];
- InitSharedLatch(&procs[i].waitLatch);
}
/*
@@ -217,9 +218,9 @@ InitProcGlobal(void)
for (i = 0; i < autovacuum_max_workers + 1; i++)
{
PGSemaphoreCreate(&(procs[i].sem));
+ InitSharedLatch(&procs[i].procLatch);
procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs;
ProcGlobal->autovacFreeProcs = &procs[i];
- InitSharedLatch(&procs[i].waitLatch);
}
/*
@@ -230,7 +231,7 @@ InitProcGlobal(void)
{
AuxiliaryProcs[i].pid = 0; /* marks auxiliary proc as not in use */
PGSemaphoreCreate(&(AuxiliaryProcs[i].sem));
- InitSharedLatch(&procs[i].waitLatch);
+ InitSharedLatch(&AuxiliaryProcs[i].procLatch);
}
/* Create ProcStructLock spinlock, too */
@@ -306,8 +307,8 @@ InitProcess(void)
MarkPostmasterChildActive();
/*
- * Initialize all fields of MyProc, except for the semaphore which was
- * prepared for us by InitProcGlobal.
+ * Initialize all fields of MyProc, except for the semaphore and latch,
+ * which were prepared for us by InitProcGlobal.
*/
SHMQueueElemInit(&(MyProc->links));
MyProc->waitStatus = STATUS_OK;
@@ -333,12 +334,17 @@ InitProcess(void)
SHMQueueInit(&(MyProc->myProcLocks[i]));
MyProc->recoveryConflictPending = false;
- /* Initialise for sync rep */
+ /* Initialize fields for sync rep */
MyProc->waitLSN.xlogid = 0;
MyProc->waitLSN.xrecoff = 0;
MyProc->syncRepState = SYNC_REP_NOT_WAITING;
SHMQueueElemInit(&(MyProc->syncRepLinks));
- OwnLatch(&MyProc->waitLatch);
+
+ /*
+ * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
+ * Note that there's no particular need to do ResetLatch here.
+ */
+ OwnLatch(&MyProc->procLatch);
/*
* We might be reusing a semaphore that belonged to a failed process. So
@@ -379,7 +385,6 @@ InitProcessPhase2(void)
/*
* Arrange to clean that up at backend exit.
*/
- on_shmem_exit(SyncRepCleanupAtProcExit, 0);
on_shmem_exit(RemoveProcFromArray, 0);
}
@@ -454,8 +459,8 @@ InitAuxiliaryProcess(void)
SpinLockRelease(ProcStructLock);
/*
- * Initialize all fields of MyProc, except for the semaphore which was
- * prepared for us by InitProcGlobal.
+ * Initialize all fields of MyProc, except for the semaphore and latch,
+ * which were prepared for us by InitProcGlobal.
*/
SHMQueueElemInit(&(MyProc->links));
MyProc->waitStatus = STATUS_OK;
@@ -476,6 +481,12 @@ InitAuxiliaryProcess(void)
SHMQueueInit(&(MyProc->myProcLocks[i]));
/*
+ * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
+ * Note that there's no particular need to do ResetLatch here.
+ */
+ OwnLatch(&MyProc->procLatch);
+
+ /*
* We might be reusing a semaphore that belonged to a failed process. So
* be careful and reinitialize its value here. (This is not strictly
* necessary anymore, but seems like a good idea for cleanliness.)
@@ -677,6 +688,9 @@ ProcKill(int code, Datum arg)
Assert(MyProc != NULL);
+ /* Make sure we're out of the sync rep lists */
+ SyncRepCleanupAtProcExit();
+
/*
* Release any LW locks I am holding. There really shouldn't be any, but
* it's cheap to check again before we cut the knees off the LWLock
@@ -684,6 +698,9 @@ ProcKill(int code, Datum arg)
*/
LWLockReleaseAll();
+ /* Release ownership of the process's latch, too */
+ DisownLatch(&MyProc->procLatch);
+
SpinLockAcquire(ProcStructLock);
/* Return PGPROC structure (and semaphore) to appropriate freelist */
@@ -739,6 +756,9 @@ AuxiliaryProcKill(int code, Datum arg)
/* Release any LW locks I am holding (see notes above) */
LWLockReleaseAll();
+ /* Release ownership of the process's latch, too */
+ DisownLatch(&MyProc->procLatch);
+
SpinLockAcquire(ProcStructLock);
/* Mark auxiliary proc no longer in use */