From 4410ff8859e4239467ba29bda8ded0fd4603d175 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Wed, 11 Jan 2023 02:13:04 +0100
Subject: [PATCH 1/2] Stop emitting open/nodata/close patterns in genbki.pl

Although opening and immediately closing the relation is not that
expensive, it's still cheaper to not touch it at all.
---
 src/backend/catalog/genbki.pl | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 4a7205472c..67d63864b3 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -549,12 +549,22 @@ EOM
 	{
 		print $def $line;
 	}
+	
+	my $opened = 0;
 
 	# Open it, unless it's a bootstrap catalog (create bootstrap does this
 	# automatically)
 	if (!$catalog->{bootstrap})
 	{
-		print $bki "open $catname\n";
+		if (($catalog_data{$catname} || 0) > 0 || $catname eq 'pg_attribute')
+		{
+			print $bki "open $catname\n";
+			$opened = 1;
+		}
+	}
+	else
+	{
+		$opened = 1;
 	}
 
 	# For pg_attribute.h, we generate data entries ourselves.
@@ -671,7 +681,11 @@ EOM
 		}
 	}
 
-	print $bki "close $catname\n";
+	if ($opened eq 1)
+	{
+		print $bki "close $catname\n";
+	}
+
 	printf $def "\n#endif\t\t\t\t\t\t\t/* %s_D_H */\n", uc $catname;
 
 	# Close and rename definition header
-- 
2.40.1

