diff --git a/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
index 5cf07d071ec..62be5cd0edd 100644
--- a/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
+++ b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl
@@ -25,6 +25,11 @@ my $attach_count2 =
   $node->safe_psql("postgres", "SELECT get_test_shmem_attach_count();");
 cmp_ok($attach_count2, '>', $attach_count1,
 	"attach callback is called in each backend");
+
+# Test a shared memory hash table too.
+my $result = $node->safe_psql("postgres", "SELECT test_shmem_hash_overflow();");
+ok($result eq "", "shmem hash works");
+
 $node->stop;
 
 ###
@@ -56,5 +61,9 @@ else
 	);
 }
 
+# Test a shared memory hash table too.
+$result = $node->safe_psql("postgres", "SELECT test_shmem_hash_overflow();");
+ok($result eq "", "shmem hash works");
+
 $node->stop;
 done_testing();
diff --git a/src/test/modules/test_shmem/test_shmem--1.0.sql b/src/test/modules/test_shmem/test_shmem--1.0.sql
index 2d01fd9256c..7b91f8c6f42 100644
--- a/src/test/modules/test_shmem/test_shmem--1.0.sql
+++ b/src/test/modules/test_shmem/test_shmem--1.0.sql
@@ -7,3 +7,7 @@
 CREATE FUNCTION get_test_shmem_attach_count()
 RETURNS pg_catalog.int4 STRICT
 AS 'MODULE_PATHNAME' LANGUAGE C;
+
+CREATE FUNCTION test_shmem_hash_overflow()
+RETURNS void STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/src/test/modules/test_shmem/test_shmem.c b/src/test/modules/test_shmem/test_shmem.c
index 9bd4012b435..da9fddc5ee5 100644
--- a/src/test/modules/test_shmem/test_shmem.c
+++ b/src/test/modules/test_shmem/test_shmem.c
@@ -20,10 +20,12 @@
 #include "fmgr.h"
 #include "miscadmin.h"
 #include "storage/shmem.h"
+#include "utils/hsearch.h"
 
 
 PG_MODULE_MAGIC;
 
+/* For testing ShmemRequestStruct */
 typedef struct TestShmemData
 {
 	int			value;
@@ -35,6 +37,21 @@ static TestShmemData *TestShmem;
 
 static bool attached_or_initialized = false;
 
+/* For testing ShmemRequestHash */
+
+/*
+ * XXX: This is chosen to be equal to HASH_SEGSIZE (256) so that we exercise
+ * the directory expansion bug.
+ */
+#define TEST_HASH_NELEMS	256
+
+typedef struct TestHashEntry
+{
+	int32		key;			/* hash key, must be first */
+} TestHashEntry;
+
+static HTAB *TestShmemHash;
+
 static void test_shmem_request(void *arg);
 static void test_shmem_init(void *arg);
 static void test_shmem_attach(void *arg);
@@ -54,6 +71,13 @@ test_shmem_request(void *arg)
 	ShmemRequestStruct(.name = "test_shmem area",
 					   .size = sizeof(TestShmemData),
 					   .ptr = (void **) &TestShmem);
+
+	ShmemRequestHash(.name = "test_shmem overflow hash",
+					 .nelems = TEST_HASH_NELEMS,
+					 .hash_info.keysize = sizeof(int32),
+					 .hash_info.entrysize = sizeof(TestHashEntry),
+					 .hash_flags = HASH_ELEM | HASH_BLOBS,
+					 .ptr = &TestShmemHash);
 }
 
 static void
@@ -99,3 +123,48 @@ get_test_shmem_attach_count(PG_FUNCTION_ARGS)
 		elog(ERROR, "shmem area not yet initialized");
 	PG_RETURN_INT32(TestShmem->attach_count);
 }
+
+/*
+ * Test a shared memory hash table.
+ *
+ * Check that the correct number of elements can be inserted with
+ * HASH_ENTER_NULL.  The hash table is expected to be empty before the call.
+ */
+PG_FUNCTION_INFO_V1(test_shmem_hash_overflow);
+Datum
+test_shmem_hash_overflow(PG_FUNCTION_ARGS)
+{
+	int			count = 0;
+	int32		key;
+	bool		found;
+	TestHashEntry *entry;
+
+	/* Assume the hash to be empty before the call */
+	if (hash_get_num_entries(TestShmemHash) != 0)
+		elog(ERROR, "hash table is not empty");
+
+	/* Fill up the hash table */
+	for (key = 0; key < TEST_HASH_NELEMS; key++)
+	{
+		entry = hash_search(TestShmemHash, &key, HASH_ENTER_NULL, &found);
+		if (found)
+			elog(ERROR, "hash entry with key %d already exists", key);
+		if (entry == NULL)
+			elog(ERROR, "hash table of size %d is full after only %d insertions",
+				 TEST_HASH_NELEMS, count);
+		count++;
+	}
+
+	/*
+	 * Try to insert one more entry.  It should now fail because the hash
+	 * table is full.
+	 */
+	entry = hash_search(TestShmemHash, &key, HASH_ENTER_NULL, &found);
+	if (found)
+		elog(ERROR, "hash entry with key %d already exists", key);
+	if (entry != NULL)
+		elog(ERROR, "hash table of size %d had space for an extra element",
+			 TEST_HASH_NELEMS);
+
+	PG_RETURN_VOID();
+}
