On 4/25/18, Tom Lane <t...@sss.pgh.pa.us> wrote:
> Andrew Dunstan <andrew.duns...@2ndquadrant.com> writes:
>> +many for rewriting in perl. Do you want to have a go at that? If not I
>> will.
>
> +1 ... I was mildly astonished that this didn't already have to happen
> as part of the bootstrap data conversion effort.  It's certainly not
> hard to imagine future extensions to the .dat file format that would
> break this script, and duplicate_oids too.  I think we should rewrite
> both of them to use the Catalog.pm infrastructure.

If we're going to use Catalog.pm for that, it seems more convenient to
expose toast and index oids directly rather than in strings formatted
specifically for the bki file, as in the attached. Thoughts?

-John Naylor
diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index 6305a2b..601d680 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -86,23 +86,13 @@ sub ParseHeader
 			# Push the data into the appropriate data structure.
 			if (/^DECLARE_TOAST\(\s*(\w+),\s*(\d+),\s*(\d+)\)/)
 			{
-				my ($toast_name, $toast_oid, $index_oid) = ($1, $2, $3);
 				push @{ $catalog{toasting} },
-				  "declare toast $toast_oid $index_oid on $toast_name\n";
+				  {name => $1, oid => $2, index_oid => $3};
 			}
 			elsif (/^DECLARE_(UNIQUE_)?INDEX\(\s*(\w+),\s*(\d+),\s*(.+)\)/)
 			{
-				my ($is_unique, $index_name, $index_oid, $using) =
-				  ($1, $2, $3, $4);
 				push @{ $catalog{indexing} },
-				  sprintf(
-					"declare %sindex %s %s %s\n",
-					$is_unique ? 'unique ' : '',
-					$index_name, $index_oid, $using);
-			}
-			elsif (/^BUILD_INDICES/)
-			{
-				push @{ $catalog{indexing} }, "build indices\n";
+				  {is_unique => $1, name => $2, oid => $3, using => $4};
 			}
 			elsif (/^CATALOG\((\w+),(\d+),(\w+)\)/)
 			{
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 9cf2626..bb6a99d 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -100,13 +100,18 @@ foreach my $header (@input_files)
 		$catalog_data{$catname} = Catalog::ParseData($datfile, $schema, 0);
 	}
 
-	foreach my $toast_decl (@{ $catalog->{toasting} })
+	foreach my $toast (@{ $catalog->{toasting} })
 	{
-		push @toast_decls, $toast_decl;
+		push @toast_decls,
+		  sprintf "declare toast %s %s on %s\n",
+		    $toast->{oid}, $toast->{index_oid}, $toast->{name};
 	}
-	foreach my $index_decl (@{ $catalog->{indexing} })
+	foreach my $index (@{ $catalog->{indexing} })
 	{
-		push @index_decls, $index_decl;
+		push @index_decls,
+		  sprintf "declare %sindex %s %s %s\n",
+			$index->{is_unique} ? 'unique ' : '',
+			$index->{name}, $index->{oid}, $index->{using};
 	}
 }
 
@@ -463,6 +468,9 @@ foreach my $declaration (@index_decls)
 	print $bki $declaration;
 }
 
+# last command in the BKI file: build the indexes declared above
+print $bki "build indices\n";
+
 
 # Now generate schemapg.h
 
diff --git a/src/include/catalog/indexing.h b/src/include/catalog/indexing.h
index 42499e2..2491582 100644
--- a/src/include/catalog/indexing.h
+++ b/src/include/catalog/indexing.h
@@ -47,7 +47,6 @@ extern void CatalogTupleDelete(Relation heapRel, ItemPointer tid);
  */
 #define DECLARE_INDEX(name,oid,decl) extern int no_such_variable
 #define DECLARE_UNIQUE_INDEX(name,oid,decl) extern int no_such_variable
-#define BUILD_INDICES
 
 
 /*
@@ -361,7 +360,4 @@ DECLARE_UNIQUE_INDEX(pg_subscription_subname_index, 6115, on pg_subscription usi
 DECLARE_UNIQUE_INDEX(pg_subscription_rel_srrelid_srsubid_index, 6117, on pg_subscription_rel using btree(srrelid oid_ops, srsubid oid_ops));
 #define SubscriptionRelSrrelidSrsubidIndexId 6117
 
-/* last step of initialization script: build the indexes declared above */
-BUILD_INDICES
-
 #endif							/* INDEXING_H */

Reply via email to