From 1522a894a9e9b6a50514f04f218ff85d812d9aff Mon Sep 17 00:00:00 2001
From: Amit Langote <amitlangote09@gmail.com>
Date: Fri, 14 Feb 2020 16:04:48 +0900
Subject: [PATCH v2] Don't require relnatts to be specified in pg_class.dat

---
 src/backend/catalog/Catalog.pm           |  4 ++++
 src/backend/catalog/genbki.pl            |  5 +++++
 src/include/catalog/pg_class.dat         | 14 ++++++++------
 src/include/catalog/reformat_dat_file.pl | 13 ++++++++++---
 4 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index 368b1dea3e..fdeb0c1855 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -341,6 +341,10 @@ sub AddDefaultValues
 		{
 			;
 		}
+		elsif ($attname eq 'relnatts')
+		{
+			;
+		}
 		elsif (defined $column->{default})
 		{
 			$row->{$attname} = $column->{default};
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 6eff045bd9..8ef10de2db 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -57,6 +57,7 @@ my %catalog_data;
 my @toast_decls;
 my @index_decls;
 my %oidcounts;
+my %catalog_ncols;
 
 foreach my $header (@ARGV)
 {
@@ -72,6 +73,7 @@ foreach my $header (@ARGV)
 	{
 		push @catnames, $catname;
 		$catalogs{$catname} = $catalog;
+		$catalog_ncols{$catname} = scalar(@$schema);
 	}
 
 	# While checking for duplicated OIDs, we ignore the pg_class OID and
@@ -179,6 +181,9 @@ my %classoids;
 foreach my $row (@{ $catalog_data{pg_class} })
 {
 	$classoids{ $row->{relname} } = $row->{oid};
+
+	# Also fill in relnatts while at it.
+	$row->{relnatts} = $catalog_ncols{$row->{relname}};
 }
 
 # collation OID lookup
diff --git a/src/include/catalog/pg_class.dat b/src/include/catalog/pg_class.dat
index 9bcf28676d..befa345a8c 100644
--- a/src/include/catalog/pg_class.dat
+++ b/src/include/catalog/pg_class.dat
@@ -14,17 +14,19 @@
 
 # Note: only bootstrap catalogs, ie those marked BKI_BOOTSTRAP, need to
 # have entries here.  Be sure that the OIDs listed here match those given in
-# their CATALOG and BKI_ROWTYPE_OID macros, and that the relnatts values are
-# correct.
+# their CATALOG and BKI_ROWTYPE_OID macros.
 
 # Note: "3" in the relfrozenxid column stands for FirstNormalTransactionId;
 # similarly, "1" in relminmxid stands for FirstMultiXactId
 
+# Note: relnatts is computed automatically by genbki.pl, so need not be
+# specified here.
+
 { oid => '1247',
   relname => 'pg_type', reltype => 'pg_type', relam => 'heap',
   relfilenode => '0', relpages => '0', reltuples => '0', relallvisible => '0',
   reltoastrelid => '0', relhasindex => 'f', relisshared => 'f',
-  relpersistence => 'p', relkind => 'r', relnatts => '31', relchecks => '0',
+  relpersistence => 'p', relkind => 'r', relchecks => '0',
   relhasrules => 'f', relhastriggers => 'f', relhassubclass => 'f',
   relrowsecurity => 'f', relforcerowsecurity => 'f', relispopulated => 't',
   relreplident => 'n', relispartition => 'f', relfrozenxid => '3',
@@ -34,7 +36,7 @@
   relname => 'pg_attribute', reltype => 'pg_attribute', relam => 'heap',
   relfilenode => '0', relpages => '0', reltuples => '0', relallvisible => '0',
   reltoastrelid => '0', relhasindex => 'f', relisshared => 'f',
-  relpersistence => 'p', relkind => 'r', relnatts => '25', relchecks => '0',
+  relpersistence => 'p', relkind => 'r', relchecks => '0',
   relhasrules => 'f', relhastriggers => 'f', relhassubclass => 'f',
   relrowsecurity => 'f', relforcerowsecurity => 'f', relispopulated => 't',
   relreplident => 'n', relispartition => 'f', relfrozenxid => '3',
@@ -44,7 +46,7 @@
   relname => 'pg_proc', reltype => 'pg_proc', relam => 'heap',
   relfilenode => '0', relpages => '0', reltuples => '0', relallvisible => '0',
   reltoastrelid => '0', relhasindex => 'f', relisshared => 'f',
-  relpersistence => 'p', relkind => 'r', relnatts => '29', relchecks => '0',
+  relpersistence => 'p', relkind => 'r', relchecks => '0',
   relhasrules => 'f', relhastriggers => 'f', relhassubclass => 'f',
   relrowsecurity => 'f', relforcerowsecurity => 'f', relispopulated => 't',
   relreplident => 'n', relispartition => 'f', relfrozenxid => '3',
@@ -54,7 +56,7 @@
   relname => 'pg_class', reltype => 'pg_class', relam => 'heap',
   relfilenode => '0', relpages => '0', reltuples => '0', relallvisible => '0',
   reltoastrelid => '0', relhasindex => 'f', relisshared => 'f',
-  relpersistence => 'p', relkind => 'r', relnatts => '33', relchecks => '0',
+  relpersistence => 'p', relkind => 'r', relchecks => '0',
   relhasrules => 'f', relhastriggers => 'f', relhassubclass => 'f',
   relrowsecurity => 'f', relforcerowsecurity => 'f', relispopulated => 't',
   relreplident => 'n', relispartition => 'f', relfrozenxid => '3',
diff --git a/src/include/catalog/reformat_dat_file.pl b/src/include/catalog/reformat_dat_file.pl
index fd4dbad67e..19d9c3ceae 100755
--- a/src/include/catalog/reformat_dat_file.pl
+++ b/src/include/catalog/reformat_dat_file.pl
@@ -184,10 +184,12 @@ sub strip_default_values
 	{
 		my $attname = $column->{name};
 
-		# It's okay if we have no oid value, since it will be assigned
-		# automatically before bootstrap.
+		# It's okay if we have no oid and relnatts value, since it will be
+		# assigned automatically before bootstrap.
 		die "strip_default_values: $catname.$attname undefined\n"
-		  if !defined $row->{$attname} and $attname ne 'oid';
+		  if !defined $row->{$attname} and
+			 $attname ne 'oid' and
+			 $attname ne 'relnatts';
 
 		if (defined $column->{default}
 			and ($row->{$attname} eq $column->{default}))
@@ -203,6 +205,11 @@ sub strip_default_values
 		delete $row->{pronargs} if defined $row->{proargtypes};
 	}
 
+	if ($catname eq 'pg_class')
+	{
+		delete $row->{relnatts} if defined $row->{relnatts};
+	}
+
 	# If a pg_type entry has an auto-generated array type, then its
 	# typarray field is a computed value too (see GenerateArrayTypes).
 	if ($catname eq 'pg_type')
-- 
2.20.1 (Apple Git-117)

