From c71e123c19c7b486182f02375a623d0d8698d680 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Wed, 17 Jan 2024 22:47:03 +0100
Subject: [PATCH v3] Make initdb -c option case insensitive

When a -c option specifies a GUC name to replace using different
case than the existing parameter it was added as a new parameter
rather than replacing the existing.  This makes -c replacements
case insensitive such that -cWORK_MEM will replace the value of
work_mem in the config.

Reported-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Discussion: https://postgr.es/m/20230928.164904.2153358973162534034.horikyota.ntt@gmail.com
---
 src/bin/initdb/initdb.c        | 19 ++++++++++++-------
 src/bin/initdb/t/001_initdb.pl | 13 +++++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index ac409b0006..edfab4ed22 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -475,11 +475,6 @@ replace_guc_value(char **lines, const char *guc_name, const char *guc_value,
 	/* prepare the replacement line, except for possible comment and newline */
 	if (mark_as_comment)
 		appendPQExpBufferChar(newline, '#');
-	appendPQExpBuffer(newline, "%s = ", guc_name);
-	if (guc_value_requires_quotes(guc_value))
-		appendPQExpBuffer(newline, "'%s'", escape_quotes(guc_value));
-	else
-		appendPQExpBufferStr(newline, guc_value);
 
 	for (i = 0; lines[i]; i++)
 	{
@@ -494,8 +489,15 @@ replace_guc_value(char **lines, const char *guc_name, const char *guc_value,
 		where = lines[i];
 		while (*where == '#' || isspace((unsigned char) *where))
 			where++;
-		if (strncmp(where, guc_name, namelen) != 0)
+		if (strncasecmp(where, guc_name, namelen) != 0)
 			continue;
+		/* make sure to preserve case of the replaced GUC */
+		appendPQExpBuffer(newline, "%.*s = ", namelen, where);
+		if (guc_value_requires_quotes(guc_value))
+			appendPQExpBuffer(newline, "'%s'", escape_quotes(guc_value));
+		else
+			appendPQExpBufferStr(newline, guc_value);
+
 		where += namelen;
 		while (isspace((unsigned char) *where))
 			where++;
@@ -559,7 +561,10 @@ replace_guc_value(char **lines, const char *guc_name, const char *guc_value,
 		 * No match, so append a new entry.  (We rely on the bootstrap server
 		 * to complain if it's not a valid GUC name.)
 		 */
-		appendPQExpBufferChar(newline, '\n');
+		if (guc_value_requires_quotes(guc_value))
+			appendPQExpBuffer(newline, "%s = '%s'\n", guc_name, escape_quotes(guc_value));
+		else
+			appendPQExpBuffer(newline, "%s = %s\n", guc_name, guc_value);
 		lines = pg_realloc_array(lines, char *, i + 2);
 		lines[i++] = newline->data;
 		lines[i] = NULL;		/* keep the array null-terminated */
diff --git a/src/bin/initdb/t/001_initdb.pl b/src/bin/initdb/t/001_initdb.pl
index 03376cc0f7..413a5eca67 100644
--- a/src/bin/initdb/t/001_initdb.pl
+++ b/src/bin/initdb/t/001_initdb.pl
@@ -199,4 +199,17 @@ command_fails(
 command_fails([ 'initdb', '--no-sync', '--set', 'foo=bar', "$tempdir/dataX" ],
 	'fails for invalid --set option');
 
+# Make sure multiple invocations of -c parameters are added case insensitive
+command_ok(
+	[
+		'initdb', '-cwork_mem=128', '-cWork_Mem=256', '-cWORK_MEM=512',
+		"$tempdir/dataY"
+	],
+	'multiple -c options with different case');
+
+my $conf = slurp_file("$tempdir/dataY/postgresql.conf");
+ok($conf !~ qr/^WORK_MEM = /m, "WORK_MEM should not be configured");
+ok($conf !~ qr/^Work_Mem = /m, "Work_Mem should not be configured");
+ok($conf =~ qr/^work_mem = 512/m, "work_mem should be in config");
+
 done_testing();
-- 
2.32.1 (Apple Git-133)

