--- /home/john/pgdev/postgresql/src/include/catalog/reformat_dat_files.pl 2018-03-27 18:41:30.097479755 +0700 +++ convert_oid2name.pl 2018-03-27 18:17:16.154549365 +0700 @@ -1,18 +1,14 @@ #!/usr/bin/perl -w #---------------------------------------------------------------------- # -# reformat_dat_files.pl -# Perl script that reads in a catalog data file and writes out -# a functionally equivalent file in a standard format. -# -# Metadata entries (if any) come first, with normal attributes -# starting on the following line, in the same order they would be in -# the actual table. +# convert_oid2name.pl +# Perl script that replaces some numeric OIDs with human readable +# macros. # # Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California # -# /src/include/catalog/reformat_dat_files.pl +# /src/include/catalog/convert_oid2name.pl # #---------------------------------------------------------------------- @@ -85,22 +81,68 @@ $catalog_data{$catname} = Catalog::ParseData($datfile, $schema, 1); } -######################################################################## -# At this point, we have read all the data. If you are modifying this -# script for bulk editing, this is a good place to build lookup tables, -# if you need to. In the following example, the "next if !ref $row" -# check below is a hack to filter out non-hash objects. This is because -# we build the lookup tables from data that we read using the -# "preserve_formatting" parameter. -# -##Index access method lookup. -#my %amnames; -#foreach my $row (@{ $catalog_data{pg_am} }) -#{ -# next if !ref $row; -# $amnames{$row->{oid}} = $row->{amname}; -#} -######################################################################## +# Build lookup tables. +# Note: the "next if !ref $row" checks below are a hack to filter out +# non-hash objects. This is because we build the lookup tables from data +# that we read using the "preserve_formatting" switch. + +# Index access method lookup. +my %amnames; +foreach my $row (@{ $catalog_data{pg_am} }) +{ + next if !ref $row; + $amnames{$row->{oid}} = $row->{amname}; +} + +# Type oid lookup. +my %typenames; +$typenames{'0'} = '0'; # Easier than adding a check at every type lookup +foreach my $row (@{ $catalog_data{pg_type} }) +{ + next if !ref $row; + $typenames{$row->{oid}} = $row->{typname}; +} + +# Opfamily oid lookup. +my %opfnames; +foreach my $row (@{ $catalog_data{pg_opfamily} }) +{ + next if !ref $row; + $opfnames{$row->{oid}} = $amnames{$row->{opfmethod}} . '/' . $row->{opfname}; +} + +# Opclass oid lookup. +my %opcnames; +foreach my $row (@{ $catalog_data{pg_opclass} }) +{ + next if !ref $row; + $opcnames{$row->{oid}} = $amnames{$row->{opcmethod}} . '/' . $row->{opcname} + if exists $row->{oid}; +} + +# Operator oid lookup. +my %opernames; +foreach my $row (@{ $catalog_data{pg_operator} }) +{ + next if !ref $row; + $opernames{$row->{oid}} = sprintf "%s(%s,%s)", + $row->{oprname}, $typenames{$row->{oprleft}}, $typenames{$row->{oprright}}; +} + +# Proc oid lookup. +my %procoids; +foreach my $row (@{ $catalog_data{pg_proc} }) +{ + next if !ref $row; + if (defined($procoids{ $row->{proname} })) + { + $procoids{ $row->{proname} } = 'MULTIPLE'; + } + else + { + $procoids{ $row->{oid} } = $row->{proname}; + } +} # Write the data. foreach my $catname (@catnames) @@ -131,17 +173,102 @@ my %values = %$data; ############################################################ - # At this point we have the full tuple in memory as a hash - # and can do any operations we want. As written, it only - # removes default values, but this script can be adopted to - # do one-off bulk-editing. - ############################################################ + # We strip default values first because at the time it seemed + # easier to check for existence rather than add sentinel values + # to the lookups. if (!$full_tuples) { strip_default_values(\%values, $schema, $catname); } + # Replace OIDs with names + + if ($catname eq 'pg_proc') + { + $values{prorettype} = $typenames{$values{prorettype}}; + if ($values{proargtypes}) + { + my @argtypeoids = split /\s+/, $values{proargtypes}; + my @argtypenames; + foreach my $argtypeoid (@argtypeoids) + { + push @argtypenames, $typenames{$argtypeoid}; + } + $values{proargtypes} = join(' ', @argtypenames); + } + if ($values{proallargtypes}) + { + $values{proallargtypes} =~ s/[{}]//g; + my @argtypeoids = split /,/, $values{proallargtypes}; + my @argtypenames; + foreach my $argtypeoid (@argtypeoids) + { + push @argtypenames, $typenames{$argtypeoid}; + } + $values{proallargtypes} = '{' . join(',', @argtypenames) . '}'; + } + } + elsif ($catname eq 'pg_aggregate') + { + $values{aggsortop} = $opernames{$values{aggsortop}} + if exists $values{aggsortop}; + $values{aggtranstype} = $typenames{$values{aggtranstype}}; + $values{aggmtranstype} = $typenames{$values{aggmtranstype}} + if exists $values{aggmtranstype}; + } + elsif ($catname eq 'pg_amop') + { + $values{amoplefttype} = $typenames{$values{amoplefttype}}; + $values{amoprighttype} = $typenames{$values{amoprighttype}}; + $values{amopmethod} = $amnames{$values{amopmethod}}; + $values{amopfamily} = $opfnames{$values{amopfamily}}; + $values{amopopr} = $opernames{$values{amopopr}}; + $values{amopsortfamily} = $opfnames{$values{amopsortfamily}} + if exists $values{amopsortfamily}; + } + elsif ($catname eq 'pg_amproc') + { + $values{amprocfamily} = $opfnames{$values{amprocfamily}}; + $values{amproclefttype} = $typenames{$values{amproclefttype}}; + $values{amprocrighttype} = $typenames{$values{amprocrighttype}}; + } + elsif ($catname eq 'pg_cast') + { + $values{castsource} = $typenames{$values{castsource}}; + $values{casttarget} = $typenames{$values{casttarget}}; + } + elsif ($catname eq 'pg_opclass') + { + $values{opcmethod} = $amnames{$values{opcmethod}}; + $values{opcfamily} = $opfnames{$values{opcfamily}}; + $values{opcintype} = $typenames{$values{opcintype}}; + $values{opckeytype} = $typenames{$values{opckeytype}} + if exists $values{opckeytype}; + } + elsif ($catname eq 'pg_operator') + { + $values{oprleft} = $typenames{$values{oprleft}}; + $values{oprright} = $typenames{$values{oprright}}; + $values{oprresult} = $typenames{$values{oprresult}}; + $values{oprcom} = $opernames{$values{oprcom}} + if exists $values{oprcom}; + $values{oprnegate} = $opernames{$values{oprnegate}} + if exists $values{oprnegate}; + } + elsif ($catname eq 'pg_opfamily') + { + $values{opfmethod} = $amnames{$values{opfmethod}}; + } + elsif ($catname eq 'pg_range') + { + $values{rngtypid} = $typenames{$values{rngtypid}}; + $values{rngsubtype} = $typenames{$values{rngsubtype}}; + $values{rngsubopc} = $opcnames{$values{rngsubopc}}; + } + + ############################################################ + print $dat "{"; # Separate out metadata fields for readability. @@ -286,7 +413,7 @@ sub usage { die <