diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index c895876..01adb8a 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -254,6 +254,8 @@ static HTAB *LockMethodLockHash;
 static HTAB *LockMethodProcLockHash;
 static HTAB *LockMethodLocalHash;
 
+/* Initial size of LockMethodLocalHash table */
+#define LOCKMETHODLOCALHASH_INIT_SIZE 16
 
 /* private state for error cleanup */
 static LOCALLOCK *StrongLockInProgress;
@@ -339,6 +341,7 @@ PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
 #endif							/* not LOCK_DEBUG */
 
 
+static void CreateLocalLockHash(void);
 static uint32 proclock_hash(const void *key, Size keysize);
 static void RemoveLocalLock(LOCALLOCK *locallock);
 static PROCLOCK *SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
@@ -431,14 +434,28 @@ InitLocks(void)
 	if (!found)
 		SpinLockInit(&FastPathStrongRelationLocks->mutex);
 
+	CreateLocalLockHash();
+}
+
+/*
+ * CreateLocalLockHash
+ *		Create the LockMethodLocalHash hash table.
+ */
+static void
+CreateLocalLockHash(void)
+{
+	HASHCTL		info;
+
 	/*
 	 * Allocate non-shared hash table for LOCALLOCK structs.  This stores lock
 	 * counts and resource owner information.
 	 *
-	 * The non-shared table could already exist in this process (this occurs
-	 * when the postmaster is recreating shared memory after a backend crash).
-	 * If so, delete and recreate it.  (We could simply leave it, since it
-	 * ought to be empty in the postmaster, but for safety let's zap it.)
+	 * First destroy any old table that may exist.  We might just be
+	 * recreating the table or it could already exist in this process (this
+	 * occurs when the postmaster is recreating shared memory after a backend
+	 * crash).  If so, delete and recreate it.  (We could simply leave it,
+	 * since it ought to be empty in the postmaster, but for safety let's zap
+	 * it.)
 	 */
 	if (LockMethodLocalHash)
 		hash_destroy(LockMethodLocalHash);
@@ -447,12 +464,11 @@ InitLocks(void)
 	info.entrysize = sizeof(LOCALLOCK);
 
 	LockMethodLocalHash = hash_create("LOCALLOCK hash",
-									  16,
+									  LOCKMETHODLOCALHASH_INIT_SIZE,
 									  &info,
 									  HASH_ELEM | HASH_BLOBS);
 }
 
-
 /*
  * Fetch the lock method table associated with a given lock
  */
@@ -2349,6 +2365,18 @@ LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
 		LWLockRelease(partitionLock);
 	}							/* loop over partitions */
 
+	/*
+	 * The hash_seq_search can become inefficient when the hash table has
+	 * grown significantly larger than the default size due to the backend
+	 * having run queries which obtained large numbers of locks at once. Here
+	 * we'll build a new table at its initial size whenever the table is empty
+	 * and the maximum used bucket is beyond the original table size.
+	 */
+	if (hash_get_num_entries(LockMethodLocalHash) == 0 &&
+		hash_get_max_bucket(LockMethodLocalHash) >
+		LOCKMETHODLOCALHASH_INIT_SIZE)
+		CreateLocalLockHash();
+
 #ifdef LOCK_DEBUG
 	if (*(lockMethodTable->trace_flag))
 		elog(LOG, "LockReleaseAll done");
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 9dc2a55..5631258 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -1352,6 +1352,15 @@ hash_get_num_entries(HTAB *hashp)
 }
 
 /*
+ * hash_get_max_bucket -- get the maximum used bucket in a hashtable
+ */
+uint32
+hash_get_max_bucket(HTAB *hashp)
+{
+	return hashp->hctl->max_bucket;
+}
+
+/*
  * hash_seq_init/_search/_term
  *			Sequentially search through hash table and return
  *			all the elements one by one, return NULL when no more.
diff --git a/src/include/utils/hsearch.h b/src/include/utils/hsearch.h
index 373854c..07c38d6 100644
--- a/src/include/utils/hsearch.h
+++ b/src/include/utils/hsearch.h
@@ -132,6 +132,7 @@ extern void *hash_search_with_hash_value(HTAB *hashp, const void *keyPtr,
 extern bool hash_update_hash_key(HTAB *hashp, void *existingEntry,
 					 const void *newKeyPtr);
 extern long hash_get_num_entries(HTAB *hashp);
+extern uint32 hash_get_max_bucket(HTAB *hashp);
 extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
 extern void *hash_seq_search(HASH_SEQ_STATUS *status);
 extern void hash_seq_term(HASH_SEQ_STATUS *status);
