From 0e7c9d362e6a3c2e49ef614ff4cdd4fcb01e7494 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Mon, 10 Nov 2025 16:35:36 +0800
Subject: [PATCH v4] gen_guc_tables.pl: Validate required GUC fields before
 code generation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously, gen_guc_tables.pl would emit "Use of uninitialized value"
warnings if required fields were missing in guc_parameters.dat (for
example, when an integer or real GUC omitted the 'max' value).  The
resulting error messages were unclear and did not identify which GUC
entry was problematic.

Add explicit validation of required fields depending on the parameter
type, and fail with a clear and specific message such as:

    error: guc_parameters.dat line 1909: 'max_index_keys' of type 'int' is missing required field 'max'

No functional changes to generated guc_tables.c.

Author: Chao Li <lic@highgo.com>
Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://postgr.es/m/305B1E2D-3E35-410B-A840-53F62EDF5045@gmail.com
---
 src/backend/utils/misc/gen_guc_tables.pl | 46 ++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/src/backend/utils/misc/gen_guc_tables.pl b/src/backend/utils/misc/gen_guc_tables.pl
index 601c34ec30b..3930720dffd 100644
--- a/src/backend/utils/misc/gen_guc_tables.pl
+++ b/src/backend/utils/misc/gen_guc_tables.pl
@@ -38,6 +38,50 @@ sub dquote
 	return q{"} . $s =~ s/"/\\"/gr . q{"};
 }
 
+sub validate_guc_entry
+{
+	my ($entry) = @_;
+
+	my @required_common = qw(name type context group short_desc variable boot_val);
+
+	# Type-specific required fields
+	my %required_by_type = (
+		int    => [qw(min max)],
+		real   => [qw(min max)],
+		enum   => [qw(options)],
+		bool   => [],   # no extra required fields
+		string => [],   # no extra required fields
+	);
+
+	# Check common required fields
+	for my $f (@required_common) {
+		unless (defined $entry->{$f}) {
+			die sprintf(
+				"error: guc_parameters.dat line %d: '%s' is missing required field '%s'\n",
+				$entry->{line_number}, $entry->{name} // '<unknown>', $f
+			);
+		}
+	}
+
+	# Check that the type is known and check type-specific fields
+	unless (exists $required_by_type{$entry->{type}}) {
+		die sprintf(
+			"error: guc_parameters.dat line %d: '%s' has unknown GUC type '%s'\n",
+			$entry->{line_number}, $entry->{name}, $entry->{type} // '<unknown>'
+		);
+	}
+
+	# Check per-type required fields
+	for my $f (@{ $required_by_type{$entry->{type}} }) {
+		unless (defined $entry->{$f}) {
+			die sprintf(
+				"error: guc_parameters.dat line %d: '%s' of type '%s' is missing required field '%s'\n",
+				$entry->{line_number}, $entry->{name}, $entry->{type}, $f
+			);
+		}
+	}
+}
+
 # Print GUC table.
 sub print_table
 {
@@ -50,6 +94,8 @@ sub print_table
 
 	foreach my $entry (@{$parse})
 	{
+		validate_guc_entry($entry);
+
 		if (defined($prev_name) && lc($prev_name) ge lc($entry->{name}))
 		{
 			die sprintf(
-- 
2.39.5 (Apple Git-154)

