0005-WIP-Generate-ObjectProperty-from-catalog-files.patch

text/plain

Filename: 0005-WIP-Generate-ObjectProperty-from-catalog-files.patch
Type: text/plain
Part: 4
Message: generate syscache info automatically

Patch

Format: format-patch
Series: patch 0005
Subject: WIP: Generate ObjectProperty from catalog files
File+
src/backend/catalog/genbki.pl 81 0
src/backend/catalog/.gitignore 1 0
src/tools/pginclude/cpluspluscheck 3 0
src/tools/pginclude/headerscheck 3 0
From 881e9f1254376c0877ad562f98ee112eab47ac33 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 29 May 2023 19:56:31 -0400
Subject: [PATCH 5/5] WIP: Generate ObjectProperty from catalog files

---
 src/backend/catalog/.gitignore     |  1 +
 src/backend/catalog/genbki.pl      | 81 ++++++++++++++++++++++++++++++
 src/tools/pginclude/cpluspluscheck |  3 ++
 src/tools/pginclude/headerscheck   |  3 ++
 4 files changed, 88 insertions(+)

diff --git a/src/backend/catalog/.gitignore b/src/backend/catalog/.gitignore
index ff65560379..d817798dc0 100644
--- a/src/backend/catalog/.gitignore
+++ b/src/backend/catalog/.gitignore
@@ -1,3 +1,4 @@
+/objectproperty_info.c.h
 /postgres.bki
 /schemapg.h
 /syscache_info.c.h
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 80a7828d6c..e0813d1286 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -19,8 +19,10 @@
 
 use FindBin;
 use lib $FindBin::RealBin;
+use lib "$FindBin::RealBin/../../tools";
 
 use Catalog;
+use PerfectHash;
 
 my $output_path = '';
 my $major_version;
@@ -59,6 +61,7 @@
 my %syscaches;
 my %oidcounts;
 my @system_constraints;
+my @object_properties;
 
 foreach my $header (@ARGV)
 {
@@ -133,6 +136,7 @@
 		$oidcounts{ $toast->{toast_oid} }++;
 		$oidcounts{ $toast->{toast_index_oid} }++;
 	}
+	my ($oid_index, $oid_syscache, $name_syscache, $is_nsp_name_unique);
 	foreach my $index (@{ $catalog->{indexing} })
 	{
 		push @index_decls,
@@ -142,6 +146,7 @@
 		  $index->{table_name},
 		  $index->{index_decl};
 		$oidcounts{ $index->{index_oid} }++;
+		$oid_index = $index->{index_oid_macro} if $index->{index_decl} eq 'btree(oid oid_ops)';
 
 		if ($index->{is_unique})
 		{
@@ -152,6 +157,11 @@
 			  $index->{index_name};
 		}
 
+		if ($index->{is_unique} && $index->{index_decl} =~ /\(\w+name name_ops, \w+namespace oid_ops\)/)
+		{
+			$is_nsp_name_unique = 1;
+		}
+
 		if ($index->{syscache_name})
 		{
 			my $tbl = $index->{table_name};
@@ -165,8 +175,51 @@
 				key => $key,
 				nbuckets => $index->{syscache_nbuckets},
 			};
+
+			$oid_syscache = $index->{syscache_name} if $index->{index_decl} eq 'btree(oid oid_ops)';
+			$name_syscache = $index->{syscache_name} if $index->{index_decl} =~ /^btree\(\w+name name_ops\)$/;
+		}
+	}
+
+	my ($attnum_oid, $attnum_name, $attnum_namespace, $attnum_owner, $attnum_acl);
+	foreach my $att (@$schema)
+	{
+		if ($att->{name} eq 'oid' && $att->{type} eq 'oid')
+		{
+			$attnum_oid = "Anum_${catname}_" . $att->{name};
+		}
+		elsif ($att->{name} =~ /name$/ && $att->{type} eq 'name')
+		{
+			$attnum_name = "Anum_${catname}_" . $att->{name};
+		}
+		elsif ($att->{name} =~ /namespace$/ && $att->{type} eq 'oid')
+		{
+			$attnum_namespace = "Anum_${catname}_" . $att->{name};
+		}
+		elsif ($att->{name} =~ /owner$/ && $att->{type} eq 'oid')
+		{
+			$attnum_owner = "Anum_${catname}_" . $att->{name};
+		}
+		elsif ($att->{name} =~ /acl$/ && $att->{type} eq '_aclitem')
+		{
+			$attnum_acl = "Anum_${catname}_" . $att->{name};
 		}
 	}
+	push @object_properties, {
+		#class_descr => q{"TODO"},
+		class_oid => $catalog->{relation_oid_macro},
+		_class_oid => $catalog->{relation_oid},
+		oid_index_oid => $oid_index,
+		oid_catcache_id => $oid_syscache || '-1',
+		name_catcache_id => $name_syscache || '-1',
+		attnum_oid => $attnum_oid,
+		attnum_name => $attnum_name,
+		attnum_namespace => $attnum_namespace,
+		attnum_owner => $attnum_owner,
+		attnum_acl => $attnum_acl,
+		#objtype => TODO,
+		is_nsp_name_unique => $is_nsp_name_unique ? 'true' : 'false',
+	};
 }
 
 # Complain and exit if we found any duplicate OIDs.
@@ -441,6 +494,9 @@
 my $syscache_info_c = $output_path . 'syscache_info.c.h';
 open my $syscache_info_c_fh, '>', $syscache_info_c . $tmpext
   or die "can't open $syscache_info_c$tmpext: $!";
+my $objectproperty_info = $output_path . 'objectproperty_info.c.h';
+open my $objectproperty_info_fh, '>', $objectproperty_info . $tmpext
+  or die "can't open $objectproperty_info$tmpext: $!";
 
 # Generate postgres.bki and pg_*_d.h headers.
 
@@ -802,6 +858,29 @@
 
 print $syscache_info_c_fh "};\n";
 
+# Now generate objectproperty_info
+
+print_boilerplate($objectproperty_info_fh, "objectproperty_info.h", "object property data");
+print $objectproperty_info_fh "static const ObjectPropertyType ObjectProperty[] =
+{
+";
+
+my @class_oids;
+foreach my $op (@object_properties)
+{
+	print $objectproperty_info_fh "\t{\n";
+	foreach my $p (sort keys %{$op})
+	{
+		printf $objectproperty_info_fh "\t\t%s = %s,\n", $p, ${$op}{$p} if $p !~ /^_/ && ${$op}{$p};
+	}
+	push @class_oids, pack('N', ${$op}{_class_oid});
+	print $objectproperty_info_fh "\t},\n";
+}
+
+print $objectproperty_info_fh "};\n";
+
+print $objectproperty_info_fh "\nstatic " . PerfectHash::generate_hash_function(\@class_oids, 'objectproperty_hash_func', fixed_key_length => 4);
+
 # We're done emitting data
 close $bki;
 close $schemapg;
@@ -809,6 +888,7 @@
 close $constraints;
 close $syscache_info_h_fh;
 close $syscache_info_c_fh;
+close $objectproperty_info_fh;
 
 # Finally, rename the completed files into place.
 Catalog::RenameTempFile($bkifile, $tmpext);
@@ -817,6 +897,7 @@
 Catalog::RenameTempFile($constraints_file, $tmpext);
 Catalog::RenameTempFile($syscache_info_h, $tmpext);
 Catalog::RenameTempFile($syscache_info_c, $tmpext);
+Catalog::RenameTempFile($objectproperty_info, $tmpext);
 
 exit($num_errors != 0 ? 1 : 0);
 
diff --git a/src/tools/pginclude/cpluspluscheck b/src/tools/pginclude/cpluspluscheck
index 56ed423567..49e3ca7a7b 100755
--- a/src/tools/pginclude/cpluspluscheck
+++ b/src/tools/pginclude/cpluspluscheck
@@ -119,6 +119,9 @@ do
 	test "$f" = src/include/common/unicode_nonspacing_table.h && continue
 	test "$f" = src/include/common/unicode_east_asian_fw_table.h && continue
 
+	test "$f" = src/backend/catalog/objectproperty_info.c.h && continue
+	test "$f" = src/include/catalog/objectproperty_info.c.h && continue
+
 	test "$f" = src/backend/catalog/syscache_info.c.h && continue
 	test "$f" = src/backend/catalog/syscache_info.h && continue
 	test "$f" = src/include/catalog/syscache_info.c.h && continue
diff --git a/src/tools/pginclude/headerscheck b/src/tools/pginclude/headerscheck
index 477e4dd7e6..a1ea2ef692 100755
--- a/src/tools/pginclude/headerscheck
+++ b/src/tools/pginclude/headerscheck
@@ -114,6 +114,9 @@ do
 	test "$f" = src/include/common/unicode_nonspacing_table.h && continue
 	test "$f" = src/include/common/unicode_east_asian_fw_table.h && continue
 
+	test "$f" = src/backend/catalog/objectproperty_info.c.h && continue
+	test "$f" = src/include/catalog/objectproperty_info.c.h && continue
+
 	test "$f" = src/backend/catalog/syscache_info.c.h && continue
 	test "$f" = src/backend/catalog/syscache_info.h && continue
 	test "$f" = src/include/catalog/syscache_info.c.h && continue
-- 
2.40.1