summaryrefslogtreecommitdiff
path: root/src/backend/catalog/genbki.pl
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/catalog/genbki.pl')
-rw-r--r--src/backend/catalog/genbki.pl123
1 files changed, 106 insertions, 17 deletions
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index b68c1752c00..5bdc7adc440 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -213,6 +213,12 @@ foreach my $row (@{ $catalog_data{pg_am} })
$amoids{ $row->{amname} } = $row->{oid};
}
+# There is only one authid at bootstrap time, and we handle it specially:
+# the usually-defaulted symbol PGUID becomes the bootstrap superuser's OID.
+# (We could drop this in favor of writing out BKI_DEFAULT(POSTGRES) ...)
+my %authidoids;
+$authidoids{'PGUID'} = $BOOTSTRAP_SUPERUSERID;
+
# class (relation) OID lookup (note this only covers bootstrap catalogs!)
my %classoids;
foreach my $row (@{ $catalog_data{pg_class} })
@@ -234,6 +240,12 @@ foreach my $row (@{ $catalog_data{pg_language} })
$langoids{ $row->{lanname} } = $row->{oid};
}
+# There is only one namespace at bootstrap time, and we handle it specially:
+# the usually-defaulted symbol PGNSP becomes the pg_catalog namespace's OID.
+# (We could drop this in favor of writing out BKI_DEFAULT(pg_catalog) ...)
+my %namespaceoids;
+$namespaceoids{'PGNSP'} = $PG_CATALOG_NAMESPACE;
+
# opclass OID lookup
my %opcoids;
foreach my $row (@{ $catalog_data{pg_opclass} })
@@ -376,9 +388,11 @@ close $ef;
# Map lookup name to the corresponding hash table.
my %lookup_kind = (
pg_am => \%amoids,
+ pg_authid => \%authidoids,
pg_class => \%classoids,
pg_collation => \%collationoids,
pg_language => \%langoids,
+ pg_namespace => \%namespaceoids,
pg_opclass => \%opcoids,
pg_operator => \%operoids,
pg_opfamily => \%opfoids,
@@ -400,6 +414,9 @@ open my $bki, '>', $bkifile . $tmpext
my $schemafile = $output_path . 'schemapg.h';
open my $schemapg, '>', $schemafile . $tmpext
or die "can't open $schemafile$tmpext: $!";
+my $fk_info_file = $output_path . 'system_fk_info.h';
+open my $fk_info, '>', $fk_info_file . $tmpext
+ or die "can't open $fk_info_file$tmpext: $!";
my $constraints_file = $output_path . 'system_constraints.sql';
open my $constraints, '>', $constraints_file . $tmpext
or die "can't open $constraints_file$tmpext: $!";
@@ -554,18 +571,14 @@ EOM
$GenbkiNextOid++;
}
- # Substitute constant values we acquired above.
- # (It's intentional that this can apply to parts of a field).
- $bki_values{$attname} =~ s/\bPGUID\b/$BOOTSTRAP_SUPERUSERID/g;
- $bki_values{$attname} =~ s/\bPGNSP\b/$PG_CATALOG_NAMESPACE/g;
-
# Replace OID synonyms with OIDs per the appropriate lookup rule.
#
# If the column type is oidvector or _oid, we have to replace
# each element of the array as per the lookup rule.
if ($column->{lookup})
{
- my $lookup = $lookup_kind{ $column->{lookup} };
+ my $lookup = $lookup_kind{ $column->{lookup} };
+ my $lookup_opt = $column->{lookup_opt};
my @lookupnames;
my @lookupoids;
@@ -575,8 +588,9 @@ EOM
if ($atttype eq 'oidvector')
{
@lookupnames = split /\s+/, $bki_values{$attname};
- @lookupoids = lookup_oids($lookup, $catname, \%bki_values,
- @lookupnames);
+ @lookupoids =
+ lookup_oids($lookup, $catname, $attname, $lookup_opt,
+ \%bki_values, @lookupnames);
$bki_values{$attname} = join(' ', @lookupoids);
}
elsif ($atttype eq '_oid')
@@ -586,8 +600,8 @@ EOM
$bki_values{$attname} =~ s/[{}]//g;
@lookupnames = split /,/, $bki_values{$attname};
@lookupoids =
- lookup_oids($lookup, $catname, \%bki_values,
- @lookupnames);
+ lookup_oids($lookup, $catname, $attname,
+ $lookup_opt, \%bki_values, @lookupnames);
$bki_values{$attname} = sprintf "{%s}",
join(',', @lookupoids);
}
@@ -595,8 +609,9 @@ EOM
else
{
$lookupnames[0] = $bki_values{$attname};
- @lookupoids = lookup_oids($lookup, $catname, \%bki_values,
- @lookupnames);
+ @lookupoids =
+ lookup_oids($lookup, $catname, $attname, $lookup_opt,
+ \%bki_values, @lookupnames);
$bki_values{$attname} = $lookupoids[0];
}
}
@@ -706,14 +721,78 @@ foreach my $table_name (@tables_needing_macros)
# Closing boilerplate for schemapg.h
print $schemapg "\n#endif\t\t\t\t\t\t\t/* SCHEMAPG_H */\n";
+# Now generate system_fk_info.h
+
+# Opening boilerplate for system_fk_info.h
+print $fk_info <<EOM;
+/*-------------------------------------------------------------------------
+ *
+ * system_fk_info.h
+ * Data about the foreign-key relationships in the system catalogs
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * NOTES
+ * ******************************
+ * *** DO NOT EDIT THIS FILE! ***
+ * ******************************
+ *
+ * It has been GENERATED by src/backend/catalog/genbki.pl
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef SYSTEM_FK_INFO_H
+#define SYSTEM_FK_INFO_H
+
+typedef struct SysFKRelationship
+{
+ Oid fk_table; /* referencing catalog */
+ Oid pk_table; /* referenced catalog */
+ const char *fk_columns; /* referencing column name(s) */
+ const char *pk_columns; /* referenced column name(s) */
+ bool is_array; /* if true, last fk_column is an array */
+ bool is_opt; /* if true, fk_column can be zero */
+} SysFKRelationship;
+
+static const SysFKRelationship sys_fk_relationships[] = {
+EOM
+
+# Emit system_fk_info data
+foreach my $catname (@catnames)
+{
+ my $catalog = $catalogs{$catname};
+ foreach my $fkinfo (@{ $catalog->{foreign_keys} })
+ {
+ my $pktabname = $fkinfo->{pk_table};
+
+ # We use BKI_LOOKUP for encodings, but there's no real catalog there
+ next if $pktabname eq 'encoding';
+
+ printf $fk_info
+ "\t{ /* %s */ %s, /* %s */ %s, \"{%s}\", \"{%s}\", %s, %s},\n",
+ $catname, $catalog->{relation_oid},
+ $pktabname, $catalogs{$pktabname}->{relation_oid},
+ $fkinfo->{fk_cols},
+ $fkinfo->{pk_cols},
+ ($fkinfo->{is_array} ? "true" : "false"),
+ ($fkinfo->{is_opt} ? "true" : "false");
+ }
+}
+
+# Closing boilerplate for system_fk_info.h
+print $fk_info "};\n\n#endif\t\t\t\t\t\t\t/* SYSTEM_FK_INFO_H */\n";
+
# We're done emitting data
close $bki;
close $schemapg;
+close $fk_info;
close $constraints;
# Finally, rename the completed files into place.
Catalog::RenameTempFile($bkifile, $tmpext);
Catalog::RenameTempFile($schemafile, $tmpext);
+Catalog::RenameTempFile($fk_info_file, $tmpext);
Catalog::RenameTempFile($constraints_file, $tmpext);
exit 0;
@@ -948,7 +1027,8 @@ sub morph_row_for_schemapg
# within this genbki.pl run.)
sub lookup_oids
{
- my ($lookup, $catname, $bki_values, @lookupnames) = @_;
+ my ($lookup, $catname, $attname, $lookup_opt, $bki_values, @lookupnames)
+ = @_;
my @lookupoids;
foreach my $lookupname (@lookupnames)
@@ -961,10 +1041,19 @@ sub lookup_oids
else
{
push @lookupoids, $lookupname;
- warn sprintf
- "unresolved OID reference \"%s\" in %s.dat line %s\n",
- $lookupname, $catname, $bki_values->{line_number}
- if $lookupname ne '-' and $lookupname ne '0';
+ if ($lookupname eq '-' or $lookupname eq '0')
+ {
+ warn sprintf
+ "invalid zero OID reference in %s.dat field %s line %s\n",
+ $catname, $attname, $bki_values->{line_number}
+ if !$lookup_opt;
+ }
+ else
+ {
+ warn sprintf
+ "unresolved OID reference \"%s\" in %s.dat field %s line %s\n",
+ $lookupname, $catname, $attname, $bki_values->{line_number};
+ }
}
}
return @lookupoids;