From 8e8c21280c6737f1588e94d439ee9512144f34e0 Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Mon, 20 Mar 2017 21:01:10 +1300
Subject: [PATCH 4/4] Small optimization for StandbyReleaseLocks

Here the code would check in if the passed in xid was a valid transaction
id for each item in the RecoveryLockList. If the xid was invalid, the loop
would end up releasing all locks in a rather round about and slightly
wasteful way.  It seems better to perform this check before the start of the
loop to further tighten the loop, which is known to be quite slow when many
AELs are stored. We can simply use StandbyReleaseAllLocks() which also
makes the code more explicit about what its trying to do.

Author: David Rowley
---
 src/backend/storage/ipc/standby.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c
index 26f6671..dc2c03f 100644
--- a/src/backend/storage/ipc/standby.c
+++ b/src/backend/storage/ipc/standby.c
@@ -644,8 +644,15 @@ StandbyReleaseLocks(TransactionId xid)
 			   *prev,
 			   *next;
 
+	/* release all locks when xid is not valid */
+	if (!TransactionIdIsValid(xid))
+	{
+		StandbyReleaseAllLocks();
+		return;
+	}
+
 	/*
-	 * Release all matching locks and remove them from list
+	 * Otherwise release all matching locks and remove them from list
 	 */
 	prev = NULL;
 	for (cell = list_head(RecoveryLockList); cell; cell = next)
@@ -654,7 +661,7 @@ StandbyReleaseLocks(TransactionId xid)
 
 		next = lnext(cell);
 
-		if (!TransactionIdIsValid(xid) || lock->xid == xid)
+		if (lock->xid == xid)
 		{
 			LOCKTAG		locktag;
 
-- 
1.9.5.msysgit.1

