diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index cd58c5faf0..f96caf61d7 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -1371,10 +1371,10 @@ TransactionIdIsInProgress(TransactionId xid)
 	static TransactionId *xids = NULL;
 	static TransactionId *other_xids;
 	XidCacheStatus *other_subxidstates;
-	int			nxids = 0;
 	ProcArrayStruct *arrayP = procArray;
 	TransactionId topxid;
 	TransactionId latestCompletedXid;
+	int			nxids = 0;
 	int			mypgxactoff;
 	int			numProcs;
 	int			j;
@@ -1455,9 +1455,9 @@ TransactionIdIsInProgress(TransactionId xid)
 	numProcs = arrayP->numProcs;
 	for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
 	{
-		int			pgprocno;
 		PGPROC	   *proc;
 		TransactionId pxid;
+		int			pgprocno;
 		int			pxids;
 
 		/* Ignore ourselves --- dealt with it above */
@@ -1600,10 +1600,11 @@ TransactionIdIsInProgress(TransactionId xid)
 bool
 TransactionIdIsActive(TransactionId xid)
 {
-	bool		result = false;
 	ProcArrayStruct *arrayP = procArray;
 	TransactionId *other_xids = ProcGlobal->xids;
+	int			numProcs;
 	int			i;
+	bool		result = false;
 
 	/*
 	 * Don't bother checking a transaction older than RecentXmin; it could not
@@ -1614,11 +1615,12 @@ TransactionIdIsActive(TransactionId xid)
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (i = 0; i < arrayP->numProcs; i++)
+	numProcs = arrayP->numProcs;
+	for (i = 0; i < numProcs; i++)
 	{
-		int			pgprocno = arrayP->pgprocnos[i];
-		PGPROC	   *proc = &allProcs[pgprocno];
+		PGPROC	   *proc;
 		TransactionId pxid;
+		int			pgprocno;
 
 		/* Fetch xid just once - see GetNewTransactionId */
 		pxid = UINT32_ACCESS_ONCE(other_xids[i]);
@@ -1626,6 +1628,8 @@ TransactionIdIsActive(TransactionId xid)
 		if (!TransactionIdIsValid(pxid))
 			continue;
 
+		pgprocno = arrayP->pgprocnos[i];
+		proc = &allProcs[pgprocno];
 		if (proc->pid == 0)
 			continue;			/* ignore prepared transactions */
 
@@ -1711,9 +1715,11 @@ static void
 ComputeXidHorizons(ComputeXidHorizonsResult *h)
 {
 	ProcArrayStruct *arrayP = procArray;
+	TransactionId *other_xids = ProcGlobal->xids;
+	uint8	   *allStatusFlags = ProcGlobal->statusFlags;
 	TransactionId kaxmin;
+	int			numProcs;
 	bool		in_recovery = RecoveryInProgress();
-	TransactionId *other_xids = ProcGlobal->xids;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
@@ -1762,14 +1768,14 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h)
 	 */
 	h->slot_xmin = procArray->replication_slot_xmin;
 	h->slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
-
-	for (int index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (int index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
-		int8		statusFlags = ProcGlobal->statusFlags[index];
 		TransactionId xid;
 		TransactionId xmin;
+		int8		statusFlags;
 
 		/* Fetch xid just once - see GetNewTransactionId */
 		xid = UINT32_ACCESS_ONCE(other_xids[index]);
@@ -1802,6 +1808,7 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h)
 		 * removed, as long as pg_subtrans is not truncated) or doing logical
 		 * decoding (which manages xmin separately, check below).
 		 */
+		statusFlags = allStatusFlags[index];
 		if (statusFlags & (PROC_IN_VACUUM | PROC_IN_LOGICAL_DECODING))
 			continue;
 
@@ -2221,24 +2228,22 @@ GetSnapshotDataReuse(Snapshot snapshot)
 Snapshot
 GetSnapshotData(Snapshot snapshot)
 {
-	ProcArrayStruct *arrayP = procArray;
-	TransactionId *other_xids = ProcGlobal->xids;
+	TransactionId *other_xids;
 	TransactionId xmin;
 	TransactionId xmax;
-	int			count = 0;
-	int			subcount = 0;
-	bool		suboverflowed = false;
-	FullTransactionId latest_completed;
 	TransactionId oldestxid;
-	int			mypgxactoff;
 	TransactionId myxid;
+	TransactionId replication_slot_xmin;
+	TransactionId replication_slot_catalog_xmin;
+	FullTransactionId latest_completed;
 	uint64		curXactCompletionCount;
-
-	TransactionId replication_slot_xmin = InvalidTransactionId;
-	TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
+	int			count;
+	int			subcount;
+	int			mypgxactoff;
+	bool		suboverflowed;
 
 	Assert(snapshot != NULL);
-
+	
 	/*
 	 * Allocating space for maxProcs xids is usually overkill; numProcs would
 	 * be sufficient.  But it seems better to do the malloc while not holding
@@ -2250,7 +2255,12 @@ GetSnapshotData(Snapshot snapshot)
 	 * xip arrays if any.  (This relies on the fact that all callers pass
 	 * static SnapshotData structs.)
 	 */
-	if (snapshot->xip == NULL)
+	if (snapshot->xip != NULL)
+	{
+		if (GetSnapshotDataReuse(snapshot))
+			return snapshot;
+	}
+	else
 	{
 		/*
 		 * First call for this snapshot. Snapshot is same size whether or not
@@ -2271,19 +2281,18 @@ GetSnapshotData(Snapshot snapshot)
 					 errmsg("out of memory")));
 	}
 
+	count = 0;
+	subcount = 0;
+	suboverflowed = false;
+
 	/*
 	 * It is sufficient to get shared lock on ProcArrayLock, even if we are
 	 * going to set MyProc->xmin.
 	 */
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	if (GetSnapshotDataReuse(snapshot))
-	{
-		LWLockRelease(ProcArrayLock);
-		return snapshot;
-	}
-
 	latest_completed = ShmemVariableCache->latestCompletedXid;
+	other_xids = ProcGlobal->xids;
 	mypgxactoff = MyProc->pgxactoff;
 	myxid = other_xids[mypgxactoff];
 	Assert(myxid == MyProc->xid);
@@ -2307,11 +2316,10 @@ GetSnapshotData(Snapshot snapshot)
 
 	if (!snapshot->takenDuringRecovery)
 	{
-		int			numProcs = arrayP->numProcs;
 		TransactionId *xip = snapshot->xip;
-		int		   *pgprocnos = arrayP->pgprocnos;
-		XidCacheStatus *subxidStates = ProcGlobal->subxidStates;
 		uint8	   *allStatusFlags = ProcGlobal->statusFlags;
+		ProcArrayStruct *arrayP = procArray;
+		int			numProcs = arrayP->numProcs;
 
 		/*
 		 * First collect set of pgxactoff/xids that need to be included in the
@@ -2348,14 +2356,6 @@ GetSnapshotData(Snapshot snapshot)
 			 */
 			Assert(TransactionIdIsNormal(xid));
 
-			/*
-			 * If the XID is >= xmax, we can skip it; such transactions will
-			 * be treated as running anyway (and any sub-XIDs will also be >=
-			 * xmax).
-			 */
-			if (!NormalTransactionIdPrecedes(xid, xmax))
-				continue;
-
 			/*
 			 * Skip over backends doing logical decoding which manages xmin
 			 * separately (check below) and ones running LAZY VACUUM.
@@ -2364,6 +2364,14 @@ GetSnapshotData(Snapshot snapshot)
 			if (statusFlags & (PROC_IN_LOGICAL_DECODING | PROC_IN_VACUUM))
 				continue;
 
+			/*
+			 * If the XID is >= xmax, we can skip it; such transactions will
+			 * be treated as running anyway (and any sub-XIDs will also be >=
+			 * xmax).
+			 */
+			if (!NormalTransactionIdPrecedes(xid, xmax))
+				continue;
+
 			if (NormalTransactionIdPrecedes(xid, xmin))
 				xmin = xid;
 
@@ -2387,6 +2395,7 @@ GetSnapshotData(Snapshot snapshot)
 			 */
 			if (!suboverflowed)
 			{
+				XidCacheStatus *subxidStates = ProcGlobal->subxidStates;
 
 				if (subxidStates[pgxactoff].overflowed)
 					suboverflowed = true;
@@ -2396,7 +2405,7 @@ GetSnapshotData(Snapshot snapshot)
 
 					if (nsubxids > 0)
 					{
-						int			pgprocno = pgprocnos[pgxactoff];
+						int			pgprocno = arrayP->pgprocnos[pgxactoff];
 						PGPROC	   *proc = &allProcs[pgprocno];
 
 						pg_read_barrier();	/* pairs with GetNewTransactionId */
@@ -2585,9 +2594,10 @@ bool
 ProcArrayInstallImportedXmin(TransactionId xmin,
 							 VirtualTransactionId *sourcevxid)
 {
-	bool		result = false;
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			index;
+	bool		result = false;
 
 	Assert(TransactionIdIsNormal(xmin));
 	if (!sourcevxid)
@@ -2596,18 +2606,21 @@ ProcArrayInstallImportedXmin(TransactionId xmin,
 	/* Get lock so source xact can't end while we're doing this */
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
-		int			pgprocno = arrayP->pgprocnos[index];
-		PGPROC	   *proc = &allProcs[pgprocno];
-		int			statusFlags = ProcGlobal->statusFlags[index];
+		PGPROC	   *proc;
 		TransactionId xid;
+		int			pgprocno;
+		int			statusFlags = ProcGlobal->statusFlags[index];
 
 		/* Ignore procs running LAZY VACUUM */
 		if (statusFlags & PROC_IN_VACUUM)
 			continue;
 
 		/* We are only interested in the specific virtual transaction. */
+		pgprocno = arrayP->pgprocnos[index];
+		proc = &allProcs[pgprocno];
 		if (proc->backendId != sourcevxid->backendId)
 			continue;
 		if (proc->lxid != sourcevxid->localTransactionId)
@@ -2663,8 +2676,8 @@ ProcArrayInstallImportedXmin(TransactionId xmin,
 bool
 ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
 {
-	bool		result = false;
 	TransactionId xid;
+	bool		result = false;
 
 	Assert(TransactionIdIsNormal(xmin));
 	Assert(proc != NULL);
@@ -2745,6 +2758,7 @@ GetRunningTransactionData(void)
 	TransactionId latestCompletedXid;
 	TransactionId oldestRunningXid;
 	TransactionId *xids;
+	int			numProcs;
 	int			index;
 	int			count;
 	int			subcount;
@@ -2794,7 +2808,8 @@ GetRunningTransactionData(void)
 	/*
 	 * Spin over procArray collecting all xids
 	 */
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		TransactionId xid;
 
@@ -2838,10 +2853,8 @@ GetRunningTransactionData(void)
 	{
 		XidCacheStatus *other_subxidstates = ProcGlobal->subxidStates;
 
-		for (index = 0; index < arrayP->numProcs; index++)
+		for (index = 0; index < numProcs; index++)
 		{
-			int			pgprocno = arrayP->pgprocnos[index];
-			PGPROC	   *proc = &allProcs[pgprocno];
 			int			nsubxids;
 
 			/*
@@ -2851,6 +2864,9 @@ GetRunningTransactionData(void)
 			nsubxids = other_subxidstates[index].count;
 			if (nsubxids > 0)
 			{
+				int			pgprocno = arrayP->pgprocnos[index];
+				PGPROC	   *proc = &allProcs[pgprocno];
+
 				/* barrier not really required, as XidGenLock is held, but ... */
 				pg_read_barrier();	/* pairs with GetNewTransactionId */
 
@@ -2914,6 +2930,7 @@ GetOldestActiveTransactionId(void)
 	ProcArrayStruct *arrayP = procArray;
 	TransactionId *other_xids = ProcGlobal->xids;
 	TransactionId oldestRunningXid;
+	int			numProcs;
 	int			index;
 
 	Assert(!RecoveryInProgress());
@@ -2933,7 +2950,8 @@ GetOldestActiveTransactionId(void)
 	 * Spin over procArray collecting all xids and subxids.
 	 */
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		TransactionId xid;
 
@@ -2978,7 +2996,6 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
 {
 	ProcArrayStruct *arrayP = procArray;
 	TransactionId oldestSafeXid;
-	int			index;
 	bool		recovery_in_progress = RecoveryInProgress();
 
 	Assert(LWLockHeldByMe(ProcArrayLock));
@@ -3001,16 +3018,16 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
 	 * slot's general xmin horizon, but the catalog horizon is only usable
 	 * when only catalog data is going to be looked at.
 	 */
-	if (TransactionIdIsValid(procArray->replication_slot_xmin) &&
-		TransactionIdPrecedes(procArray->replication_slot_xmin,
+	if (TransactionIdIsValid(arrayP->replication_slot_xmin) &&
+		TransactionIdPrecedes(arrayP->replication_slot_xmin,
 							  oldestSafeXid))
-		oldestSafeXid = procArray->replication_slot_xmin;
+		oldestSafeXid = arrayP->replication_slot_xmin;
 
 	if (catalogOnly &&
-		TransactionIdIsValid(procArray->replication_slot_catalog_xmin) &&
-		TransactionIdPrecedes(procArray->replication_slot_catalog_xmin,
+		TransactionIdIsValid(arrayP->replication_slot_catalog_xmin) &&
+		TransactionIdPrecedes(arrayP->replication_slot_catalog_xmin,
 							  oldestSafeXid))
-		oldestSafeXid = procArray->replication_slot_catalog_xmin;
+		oldestSafeXid = arrayP->replication_slot_catalog_xmin;
 
 	/*
 	 * If we're not in recovery, we walk over the procarray and collect the
@@ -3027,11 +3044,14 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
 	if (!recovery_in_progress)
 	{
 		TransactionId *other_xids = ProcGlobal->xids;
+		int			numProcs;
+		int			index;
 
 		/*
 		 * Spin over procArray collecting min(ProcGlobal->xids[i])
 		 */
-		for (index = 0; index < arrayP->numProcs; index++)
+		numProcs = arrayP->numProcs;
+		for (index = 0; index < numProcs; index++)
 		{
 			TransactionId xid;
 
@@ -3076,6 +3096,7 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
 {
 	VirtualTransactionId *vxids;
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			count = 0;
 	int			index;
 
@@ -3086,8 +3107,8 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
 		palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
-
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3120,15 +3141,16 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
 bool
 HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
 {
-	bool		result = false;
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			index;
 
 	Assert(type != 0);
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3145,18 +3167,16 @@ HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
 			{
 				if (VirtualTransactionIdEquals(vxid, vxids[i]))
 				{
-					result = true;
-					break;
+					LWLockRelease(ProcArrayLock);
+					return true;
 				}
 			}
-			if (result)
-				break;
 		}
 	}
 
 	LWLockRelease(ProcArrayLock);
 
-	return result;
+	return false;
 }
 
 /*
@@ -3192,25 +3212,25 @@ BackendPidGetProc(int pid)
 PGPROC *
 BackendPidGetProcWithLock(int pid)
 {
-	PGPROC	   *result = NULL;
-	ProcArrayStruct *arrayP = procArray;
+	ProcArrayStruct *arrayP;
+	int			numProcs;
 	int			index;
 
 	if (pid == 0)				/* never match dummy PGPROCs */
 		return NULL;
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	arrayP = procArray;
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
-		PGPROC	   *proc = &allProcs[arrayP->pgprocnos[index]];
+		int			pgprocno = arrayP->pgprocnos[index];
+		PGPROC	   *proc = &allProcs[pgprocno];
 
 		if (proc->pid == pid)
-		{
-			result = proc;
-			break;
-		}
+			return proc;
 	}
 
-	return result;
+	return NULL;
 }
 
 /*
@@ -3229,23 +3249,29 @@ BackendPidGetProcWithLock(int pid)
 int
 BackendXidGetPid(TransactionId xid)
 {
-	int			result = 0;
-	ProcArrayStruct *arrayP = procArray;
-	TransactionId *other_xids = ProcGlobal->xids;
+	ProcArrayStruct *arrayP;
+	TransactionId *other_xids;
+	int			numProcs;
 	int			index;
+	int			result;
 
 	if (xid == InvalidTransactionId)	/* never match invalid xid */
 		return 0;
 
+	arrayP = procArray;
+ 	other_xids = ProcGlobal->xids;
+ 	result = 0;
+
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
-		int			pgprocno = arrayP->pgprocnos[index];
-		PGPROC	   *proc = &allProcs[pgprocno];
-
 		if (other_xids[index] == xid)
 		{
+			int			pgprocno = arrayP->pgprocnos[index];
+			PGPROC	   *proc = &allProcs[pgprocno];
+
 			result = proc->pid;
 			break;
 		}
@@ -3301,6 +3327,7 @@ GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
 {
 	VirtualTransactionId *vxids;
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			count = 0;
 	int			index;
 
@@ -3310,15 +3337,17 @@ GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
-		uint8		statusFlags = ProcGlobal->statusFlags[index];
+		uint8		statusFlags;
 
 		if (proc == MyProc)
 			continue;
 
+		statusFlags = ProcGlobal->statusFlags[index];
 		if (excludeVacuum & statusFlags)
 			continue;
 
@@ -3387,6 +3416,7 @@ GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
 {
 	static VirtualTransactionId *vxids;
 	ProcArrayStruct *arrayP = procArray;
+	int 		numProcs;
 	int			count = 0;
 	int			index;
 
@@ -3407,7 +3437,8 @@ GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3467,12 +3498,14 @@ SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode,
 						 bool conflictPending)
 {
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			index;
 	pid_t		pid = 0;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3514,8 +3547,9 @@ SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode,
 bool
 MinimumActiveBackends(int min)
 {
-	ProcArrayStruct *arrayP = procArray;
-	int			count = 0;
+	ProcArrayStruct *arrayP;
+	int			numProcs;
+	int			count;
 	int			index;
 
 	/* Quick short-circuit if no minimum is specified */
@@ -3527,7 +3561,10 @@ MinimumActiveBackends(int min)
 	 * bogus, but since we are only testing fields for zero or nonzero, it
 	 * should be OK.  The result is only used for heuristic purposes anyway...
 	 */
-	for (index = 0; index < arrayP->numProcs; index++)
+	count = 0; 
+	arrayP = procArray;
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3568,12 +3605,14 @@ int
 CountDBBackends(Oid databaseid)
 {
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			count = 0;
 	int			index;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3598,12 +3637,14 @@ int
 CountDBConnections(Oid databaseid)
 {
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			count = 0;
 	int			index;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3629,12 +3670,14 @@ void
 CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
 {
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			index;
 
 	/* tell all backends to die */
 	LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3669,12 +3712,14 @@ int
 CountUserBackends(Oid roleid)
 {
 	ProcArrayStruct *arrayP = procArray;
+	int			numProcs;
 	int			count = 0;
 	int			index;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (index = 0; index < arrayP->numProcs; index++)
+	numProcs = arrayP->numProcs;
+	for (index = 0; index < numProcs; index++)
 	{
 		int			pgprocno = arrayP->pgprocnos[index];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3728,8 +3773,9 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
 	for (tries = 0; tries < 50; tries++)
 	{
 		int			nautovacs = 0;
-		bool		found = false;
+		int			numProcs;
 		int			index;
+		bool		found = false;
 
 		CHECK_FOR_INTERRUPTS();
 
@@ -3737,11 +3783,11 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
 
 		LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-		for (index = 0; index < arrayP->numProcs; index++)
+		numProcs = arrayP->numProcs;
+		for (index = 0; index < numProcs; index++)
 		{
 			int			pgprocno = arrayP->pgprocnos[index];
 			PGPROC	   *proc = &allProcs[pgprocno];
-			uint8		statusFlags = ProcGlobal->statusFlags[index];
 
 			if (proc->databaseId != databaseId)
 				continue;
@@ -3754,6 +3800,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
 				(*nprepared)++;
 			else
 			{
+				uint8		statusFlags = ProcGlobal->statusFlags[index];
+
 				(*nbackends)++;
 				if ((statusFlags & PROC_IS_AUTOVACUUM) &&
 					nautovacs < MAXAUTOVACPIDS)
@@ -3798,12 +3846,14 @@ TerminateOtherDBBackends(Oid databaseId)
 {
 	ProcArrayStruct *arrayP = procArray;
 	List	   *pids = NIL;
+	int			numProcs;
 	int			nprepared = 0;
 	int			i;
 
 	LWLockAcquire(ProcArrayLock, LW_SHARED);
 
-	for (i = 0; i < procArray->numProcs; i++)
+	numProcs = arrayP->numProcs;
+	for (i = 0; i < numProcs; i++)
 	{
 		int			pgprocno = arrayP->pgprocnos[i];
 		PGPROC	   *proc = &allProcs[pgprocno];
@@ -3951,9 +4001,9 @@ XidCacheRemoveRunningXids(TransactionId xid,
 						  int nxids, const TransactionId *xids,
 						  TransactionId latestXid)
 {
+	XidCacheStatus *mysubxidstat;
 	int			i,
 				j;
-	XidCacheStatus *mysubxidstat;
 
 	Assert(TransactionIdIsValid(xid));
 
diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c
index 5bc2a15160..37259a4c31 100644
--- a/src/backend/utils/time/snapmgr.c
+++ b/src/backend/utils/time/snapmgr.c
@@ -1800,8 +1800,6 @@ TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,
 	TransactionId xlimit = recentXmin;
 	TransactionId latest_xmin;
 	TimestampTz next_map_update_ts;
-	TransactionId threshold_timestamp;
-	TransactionId threshold_xid;
 
 	Assert(TransactionIdIsNormal(recentXmin));
 	Assert(OldSnapshotThresholdActive());
@@ -1839,6 +1837,9 @@ TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,
 	}
 	else
 	{
+		TransactionId threshold_timestamp;
+		TransactionId threshold_xid;
+
 		ts = AlignTimestampToMinuteBoundary(ts)
 			- (old_snapshot_threshold * USECS_PER_MINUTE);
 
@@ -1901,8 +1902,6 @@ void
 MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
 {
 	TimestampTz ts;
-	TransactionId latest_xmin;
-	TimestampTz update_ts;
 	bool		map_update_required = false;
 
 	/* Never call this function when old snapshot checking is disabled. */
@@ -1915,14 +1914,12 @@ MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
 	 * a new value when we have crossed a bucket boundary.
 	 */
 	SpinLockAcquire(&oldSnapshotControl->mutex_latest_xmin);
-	latest_xmin = oldSnapshotControl->latest_xmin;
-	update_ts = oldSnapshotControl->next_map_update;
-	if (ts > update_ts)
+	if (ts > oldSnapshotControl->next_map_update)
 	{
 		oldSnapshotControl->next_map_update = ts;
 		map_update_required = true;
 	}
-	if (TransactionIdFollows(xmin, latest_xmin))
+	if (TransactionIdFollows(xmin, oldSnapshotControl->latest_xmin))
 		oldSnapshotControl->latest_xmin = xmin;
 	SpinLockRelease(&oldSnapshotControl->mutex_latest_xmin);
 
@@ -2284,8 +2281,6 @@ RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
 bool
 XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
 {
-	uint32		i;
-
 	/*
 	 * Make a quick range check to eliminate most XIDs without looking at the
 	 * xip arrays.  Note that this is OK even if we convert a subxact XID to
@@ -2307,6 +2302,8 @@ XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
 	 */
 	if (!snapshot->takenDuringRecovery)
 	{
+		uint32		i;
+
 		/*
 		 * If the snapshot contains full subxact data, the fastest way to
 		 * check things is just to compare the given XID against both subxact
