Re: generate syscache info automatically

Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>

From: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2023-05-31T17:02:57Z
Lists: pgsql-hackers
Peter Eisentraut <peter@eisentraut.org> writes:

> The idea was mentioned in [0].  genbki.pl already knows everything about
> system catalog indexes.  If we add a "please also make a syscache for 
> this one" flag to the catalog metadata, we can have genbki.pl produce
> the tables in syscache.c and syscache.h automatically.

+1 on this worthwhile reduction of manual work.  Tangentially, it
reminded me of one of my least favourite parts of Catalog.pm, the
regexes in ParseHeader():


> diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
> index 84aaeb002a..a727d692b7 100644
> --- a/src/backend/catalog/Catalog.pm
> +++ b/src/backend/catalog/Catalog.pm
> @@ -110,7 +110,7 @@ sub ParseHeader
>  			  };
>  		}
>  		elsif (
> -			/^DECLARE_(UNIQUE_)?INDEX(_PKEY)?\(\s*(\w+),\s*(\d+),\s*(\w+),\s*(.+)\)/
> +			/^DECLARE_(UNIQUE_)?INDEX(_PKEY)?\(\s*(\w+),\s*(\d+),\s*(\w+),\s*(\w+),\s*(.+)\)/
>  		  )
>  		{
>  			push @{ $catalog{indexing} },
> @@ -120,7 +120,8 @@ sub ParseHeader
>  				index_name => $3,
>  				index_oid => $4,
>  				index_oid_macro => $5,
> -				index_decl => $6
> +				table_name => $6,
> +				index_decl => $7
>  			  };
>  		}
>  		elsif (/^DECLARE_OID_DEFINING_MACRO\(\s*(\w+),\s*(\d+)\)/)


Now that we require Perl 5.14, we could replace this parenthesis-
counting nightmare with named captures (introduced in Perl 5.10), which
would make the above change look like this instead (context expanded to
show the whole elsif block):

  		elsif (
 			/^DECLARE_(UNIQUE_)?INDEX(_PKEY)?\(\s*
 			 (?<index_name>\w+),\s*
 			 (?<index_oid>\d+),\s*
 			 (?<index_oid_macro>\w+),\s*
+			 (?<table_name>\w+),\s*
 			 (?<index_decl>.+)
 			 \)/x
 		  )
 		{
 			push @{ $catalog{indexing} },
 			  {
 				is_unique => $1 ? 1 : 0,
 				is_pkey => $2 ? 1 : 0,
 				%+,
 			  };
 		}

For other patterns without the optional bits in the keyword, it becomes
even simpler, e.g.

		if (/^DECLARE_TOAST\(\s*
			 (?<parent_table>\w+),\s*
			 (?<toast_oid>\d+),\s*
			 (?<toast_index_oid>\d+)\s*
			 \)/x
		  )
		{
			push @{ $catalog{toasting} }, {%+};
	   	}


I'd be happy to submit a patch to do this for all the ParseHeader()
regexes (in a separate thread) if others agree this is an improvement.

- ilmari



Commits

  1. Generate syscache info from catalog files

  2. Fill in more of ObjectProperty

  3. Correct ObjectProperty entry for transforms

  4. genbki.pl: Factor out boilerplate generation

  5. Restructure DECLARE_INDEX arguments

  6. Update DECLARE_INDEX documentation

  7. Replace hardcoded switch in object_exists() with a lookup table.