diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index e4afd07bfe..e4bf1e1cd2 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -5473,14 +5473,14 @@ valid_custom_variable_name(const char *name)
 			saw_sep = true;
 			name_start = true;
 		}
-		else if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+		else if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
 						"abcdefghijklmnopqrstuvwxyz", *p) != NULL ||
 				 IS_HIGHBIT_SET(*p))
 		{
 			/* okay as first or non-first character */
 			name_start = false;
 		}
-		else if (!name_start && strchr("0123456789_$", *p) != NULL)
+		else if (!name_start && strchr("0123456789$", *p) != NULL)
 			 /* okay as non-first character */ ;
 		else
 			return false;
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 3de6404ba5..ed17408f06 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -548,6 +548,13 @@ ERROR:  invalid configuration parameter name "special.weird name"
 DETAIL:  Custom parameter names must be two or more simple identifiers separated by dots.
 SHOW special."weird name";
 ERROR:  unrecognized configuration parameter "special.weird name"
+SET custom._my_guc = 50;
+SHOW custom._my_guc;
+ custom._my_guc 
+----------------
+ 50
+(1 row)
+
 -- Check what happens when you try to set a "custom" GUC within the
 -- namespace of an extension.
 SET plpgsql.extra_foo_warnings = true;  -- allowed if plpgsql is not loaded yet
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index d5db101e48..04da5ff7a6 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -162,6 +162,8 @@ SET custom."bad-guc" = 42;  -- disallowed because -c cannot set this name
 SHOW custom."bad-guc";
 SET special."weird name" = 'foo';  -- could be allowed, but we choose not to
 SHOW special."weird name";
+SET custom._my_guc = 50;
+SHOW custom._my_guc;
 
 -- Check what happens when you try to set a "custom" GUC within the
 -- namespace of an extension.
