From 8a0c1a7921d8dfb7baa98b4bb090239647ded7a5 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Sat, 16 Jul 2022 12:00:11 +0700
Subject: [PATCH v2 2/4] Distinguish C name and C type from SQL name and SQL
 type in Catalog.pm's interface.

A future commit will need access to the C type. The C member name shouldn't
be needed, since having "[1]" is probably not useful, but include it for
the sake of symmetry.
---
 src/backend/catalog/Catalog.pm           | 32 +++++++++++++++-------
 src/backend/catalog/genbki.pl            | 34 ++++++++++++------------
 src/include/catalog/reformat_dat_file.pl |  4 +--
 3 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index e91a8e10a8..206cb21ddc 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -190,27 +190,39 @@ sub ParseHeader
 			{
 				my %column;
 				my @attopts = split /\s+/, $_;
-				my $atttype = shift @attopts;
-				my $attname = shift @attopts;
+				my $ctype = shift @attopts;
+				my $cname = shift @attopts;
+				my $atttype;
+				my $attname;
 				die "parse error ($input_file)"
-				  unless ($attname and $atttype);
+				  unless ($cname and $ctype);
 
-				if (exists $RENAME_ATTTYPE{$atttype})
+				if (exists $RENAME_ATTTYPE{$ctype})
 				{
-					$atttype = $RENAME_ATTTYPE{$atttype};
+					$atttype = $RENAME_ATTTYPE{$ctype};
+				}
+				else
+				{
+					$atttype = $ctype;
 				}
 
 				# If the C name ends with '[]' or '[digits]', we have
 				# an array type, so we discard that from the name and
 				# prepend '_' to the type.
-				if ($attname =~ /(\w+)\[\d*\]/)
+				if ($cname =~ /(\w+)\[\d*\]/)
 				{
 					$attname = $1;
 					$atttype = '_' . $atttype;
 				}
+				else
+				{
+					$attname = $cname;
+				}
 
-				$column{type}      = $atttype;
-				$column{name}      = $attname;
+				$column{ctype}     = $ctype;
+				$column{cname}      = $cname;
+				$column{atttype}   = $atttype;
+				$column{attname}      = $attname;
 				$column{is_varlen} = 1 if $is_varlen;
 
 				foreach my $attopt (@attopts)
@@ -383,7 +395,7 @@ sub AddDefaultValues
 	# Now fill in defaults, and note any columns that remain undefined.
 	foreach my $column (@$schema)
 	{
-		my $attname = $column->{name};
+		my $attname = $column->{attname};
 
 		# No work if field already has a value.
 		next if defined $row->{$attname};
@@ -441,7 +453,7 @@ sub GenerateArrayTypes
 		# Fill in the rest of the array entry's fields.
 		foreach my $column (@$pgtype_schema)
 		{
-			my $attname = $column->{name};
+			my $attname = $column->{attname};
 
 			# Skip if we already set it above.
 			next if defined $array_type{$attname};
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 17b2c5e3f3..932f3a9ab5 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -512,8 +512,8 @@ EOM
 	foreach my $column (@$schema)
 	{
 		$attnum++;
-		my $attname = $column->{name};
-		my $atttype = $column->{type};
+		my $attname = $column->{attname};
+		my $atttype = $column->{atttype};
 
 		# Build hash of column names for use later
 		$attnames{$attname} = 1;
@@ -585,8 +585,8 @@ EOM
 		# Perform required substitutions on fields
 		foreach my $column (@$schema)
 		{
-			my $attname = $column->{name};
-			my $atttype = $column->{type};
+			my $attname = $column->{attname};
+			my $atttype = $column->{atttype};
 
 			# Assign oid if oid column exists and no explicit assignment in row
 			if ($attname eq "oid" and not defined $bki_values{$attname})
@@ -830,7 +830,7 @@ sub gen_pg_attribute
 	my @attnames;
 	foreach my $column (@$schema)
 	{
-		push @attnames, $column->{name};
+		push @attnames, $column->{attname};
 	}
 
 	foreach my $table_name (@catnames)
@@ -877,12 +877,12 @@ sub gen_pg_attribute
 		{
 			$attnum = 0;
 			my @SYS_ATTRS = (
-				{ name => 'ctid',     type => 'tid' },
-				{ name => 'xmin',     type => 'xid' },
-				{ name => 'cmin',     type => 'cid' },
-				{ name => 'xmax',     type => 'xid' },
-				{ name => 'cmax',     type => 'cid' },
-				{ name => 'tableoid', type => 'oid' });
+				{ attname => 'ctid',     atttype => 'tid' },
+				{ attname => 'xmin',     atttype => 'xid' },
+				{ attname => 'cmin',     atttype => 'cid' },
+				{ attname => 'xmax',     atttype => 'xid' },
+				{ attname => 'cmax',     atttype => 'cid' },
+				{ attname => 'tableoid', atttype => 'oid' });
 			foreach my $attr (@SYS_ATTRS)
 			{
 				$attnum--;
@@ -908,8 +908,8 @@ sub gen_pg_attribute
 sub morph_row_for_pgattr
 {
 	my ($row, $pgattr_schema, $attr, $priorfixedwidth) = @_;
-	my $attname = $attr->{name};
-	my $atttype = $attr->{type};
+	my $attname = $attr->{attname};
+	my $atttype = $attr->{atttype};
 
 	$row->{attname} = $attname;
 
@@ -969,8 +969,8 @@ sub print_bki_insert
 
 	foreach my $column (@$schema)
 	{
-		my $attname   = $column->{name};
-		my $atttype   = $column->{type};
+		my $attname   = $column->{attname};
+		my $atttype   = $column->{atttype};
 		my $bki_value = $row->{$attname};
 
 		# Fold backslash-zero to empty string if it's the entire string,
@@ -1007,8 +1007,8 @@ sub morph_row_for_schemapg
 
 	foreach my $column (@$pgattr_schema)
 	{
-		my $attname = $column->{name};
-		my $atttype = $column->{type};
+		my $attname = $column->{attname};
+		my $atttype = $column->{atttype};
 
 		# Some data types have special formatting rules.
 		if ($atttype eq 'name')
diff --git a/src/include/catalog/reformat_dat_file.pl b/src/include/catalog/reformat_dat_file.pl
index e4a105ebf5..5c03e79ccb 100755
--- a/src/include/catalog/reformat_dat_file.pl
+++ b/src/include/catalog/reformat_dat_file.pl
@@ -104,7 +104,7 @@ foreach my $catname (@catnames)
 
 	foreach my $column (@$schema)
 	{
-		my $attname = $column->{name};
+		my $attname = $column->{attname};
 
 		# We may have ordinary columns at the storage level that we still
 		# want to format as a special value. Exclude these from the column
@@ -182,7 +182,7 @@ sub strip_default_values
 	# Delete values that match defaults.
 	foreach my $column (@$schema)
 	{
-		my $attname = $column->{name};
+		my $attname = $column->{attname};
 
 		# It's okay if we have no oid value, since it will be assigned
 		# automatically before bootstrap.
-- 
2.36.1

