From b44faffd9e3b051391e0d123f407ff1d7e1f47d2 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Fri, 27 Nov 2026 13:02:01 +0200
Subject: [PATCH v15 7/8] Hold spinlock while clearing MyProc->pid on backend
 exit

That's what auxiliary proc exit did already. This makes no difference
now, but allows the next commit to atomically check that a PGPROC
entry is in use by given pid, and set an interrupt bit on its PGPROC
entry, without risk that the process exits and the entry is reused for
another process in the meanwhile.
---
 src/backend/storage/lmgr/proc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index d4a830181d8..f9175c72cc1 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -1051,12 +1051,13 @@ ProcKill(int code, Datum arg)
 	MyProc = NULL;
 	MyProcNumber = INVALID_PROC_NUMBER;
 
+	SpinLockAcquire(&ProcGlobal->freeProcsLock);
+
 	/* Mark the proc no longer in use */
 	proc->pid = 0;
 	proc->vxid.procNumber = INVALID_PROC_NUMBER;
 	proc->vxid.lxid = InvalidTransactionId;
 
-	SpinLockAcquire(&ProcGlobal->freeProcsLock);
 	if (push_leader)
 	{
 		/* Return leader PGPROC (and semaphore) to appropriate freelist */
-- 
2.47.3

