Debian Codegraph @

src/backend/catalog/genbki.pl

raw
1 #!/usr/bin/perl
2 #----------------------------------------------------------------------
3 #
4 # genbki.pl
5 # Perl script that generates postgres.bki and symbol definition
6 # headers from specially formatted header files and data files.
7 # postgres.bki is used to initialize the postgres template database.
8 #
9 # Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 # Portions Copyright (c) 1994, Regents of the University of California
11 #
12 # src/backend/catalog/genbki.pl
13 #
14 #----------------------------------------------------------------------
15
16 use strict;
17 use warnings FATAL => 'all';
18 use Getopt::Long;
19
20 use FindBin;
21 use lib $FindBin::RealBin;
22
23 use Catalog;
24
25 my $output_path = '';
26 my $major_version;
27 my $include_path;
28
29 my $num_errors = 0;
30
31 GetOptions(
32 'output:s' => \$output_path,
33 'set-version:s' => \$major_version,
34 'include-path:s' => \$include_path) || usage();
35
36 # Sanity check arguments.
37 die "No input files.\n" unless @ARGV;
38 die "--set-version must be specified.\n" unless $major_version;
39 die "Invalid version string: $major_version\n"
40 unless $major_version =~ /^\d+$/;
41 die "--include-path must be specified.\n" unless $include_path;
42
43 # Make sure paths end with a slash.
44 if ($output_path ne '' && substr($output_path, -1) ne '/')
45 {
46 $output_path .= '/';
47 }
48 if (substr($include_path, -1) ne '/')
49 {
50 $include_path .= '/';
51 }
52
53 # Read all the files into internal data structures.
54 my @catnames;
55 my %catalogs;
56 my %catalog_data;
57 my @toast_decls;
58 my @index_decls;
59 my %syscaches;
60 my %syscache_catalogs;
61 my %oidcounts;
62 my @system_constraints;
63
64 foreach my $header (@ARGV)
65 {
66 $header =~ /(.+)\.h$/
67 or die "Input files need to be header files.\n";
68 my $datfile = "$1.dat";
69
70 my $catalog = Catalog::ParseHeader($header);
71 my $catname = $catalog->{catname};
72 my $schema = $catalog->{columns};
73
74 if (defined $catname)
75 {
76 push @catnames, $catname;
77 $catalogs{$catname} = $catalog;
78 }
79
80 # While checking for duplicated OIDs, we ignore the pg_class OID and
81 # rowtype OID of bootstrap catalogs, as those are expected to appear
82 # in the initial data for pg_class and pg_type. For regular catalogs,
83 # include these OIDs. (See also Catalog::FindAllOidsFromHeaders
84 # if you change this logic.)
85 if (!$catalog->{bootstrap})
86 {
87 $oidcounts{ $catalog->{relation_oid} }++
88 if ($catalog->{relation_oid});
89 $oidcounts{ $catalog->{rowtype_oid} }++
90 if ($catalog->{rowtype_oid});
91 }
92
93 # Not all catalogs have a data file.
94 if (-e $datfile)
95 {
96 my $data = Catalog::ParseData($datfile, $schema, 0);
97 $catalog_data{$catname} = $data;
98
99 foreach my $row (@$data)
100 {
101 # Generate entries for pg_description and pg_shdescription.
102 if (defined $row->{descr})
103 {
104 my %descr = (
105 objoid => $row->{oid},
106 classoid => $catalog->{relation_oid},
107 objsubid => 0,
108 description => $row->{descr});
109
110 if ($catalog->{shared_relation})
111 {
112 delete $descr{objsubid};
113 push @{ $catalog_data{pg_shdescription} }, \%descr;
114 }
115 else
116 {
117 push @{ $catalog_data{pg_description} }, \%descr;
118 }
119 }
120
121 # Check for duplicated OIDs while we're at it.
122 $oidcounts{ $row->{oid} }++ if defined $row->{oid};
123 }
124 }
125
126 # Lookup table to get index info by index name
127 my %indexes;
128
129 # If the header file contained toast or index info, build BKI
130 # commands for those, which we'll output later.
131 foreach my $toast (@{ $catalog->{toasting} })
132 {
133 push @toast_decls,
134 sprintf "declare toast %s %s on %s\n",
135 $toast->{toast_oid}, $toast->{toast_index_oid},
136 $toast->{parent_table};
137 $oidcounts{ $toast->{toast_oid} }++;
138 $oidcounts{ $toast->{toast_index_oid} }++;
139 }
140 foreach my $index (@{ $catalog->{indexing} })
141 {
142 $indexes{ $index->{index_name} } = $index;
143
144 push @index_decls,
145 sprintf "declare %sindex %s %s on %s using %s\n",
146 $index->{is_unique} ? 'unique ' : '',
147 $index->{index_name}, $index->{index_oid},
148 $index->{table_name},
149 $index->{index_decl};
150 $oidcounts{ $index->{index_oid} }++;
151
152 if ($index->{is_unique})
153 {
154 push @system_constraints,
155 sprintf "ALTER TABLE %s ADD %s USING INDEX %s;",
156 $index->{table_name},
157 $index->{is_pkey} ? "PRIMARY KEY" : "UNIQUE",
158 $index->{index_name};
159 }
160 }
161
162 # Analyze syscache info
163 foreach my $syscache (@{ $catalog->{syscaches} })
164 {
165 my $index = $indexes{ $syscache->{index_name} };
166 my $tblname = $index->{table_name};
167 my $key = $index->{index_decl};
168 $key =~ s/^\w+\(//;
169 $key =~ s/\)$//;
170 $key =~ s/(\w+)\s+\w+/Anum_${tblname}_$1/g;
171
172 $syscaches{ $syscache->{syscache_name} } = {
173 table_oid_macro => $catalogs{$tblname}->{relation_oid_macro},
174 index_oid_macro => $index->{index_oid_macro},
175 key => $key,
176 nbuckets => $syscache->{syscache_nbuckets},
177 };
178
179 $syscache_catalogs{$catname} = 1;
180 }
181 }
182
183 # Complain and exit if we found any duplicate OIDs.
184 # While duplicate OIDs would only cause a failure if they appear in
185 # the same catalog, our project policy is that manually assigned OIDs
186 # should be globally unique, to avoid confusion.
187 my $found = 0;
188 foreach my $oid (keys %oidcounts)
189 {
190 next unless $oidcounts{$oid} > 1;
191 print STDERR "Duplicate OIDs detected:\n" if !$found;
192 print STDERR "$oid\n";
193 $found++;
194 }
195 die "found $found duplicate OID(s) in catalog data\n" if $found;
196
197
198 # OIDs not specified in the input files are automatically assigned,
199 # starting at FirstGenbkiObjectId, extending up to FirstUnpinnedObjectId.
200 # We allow such OIDs to be assigned independently within each catalog.
201 my $FirstGenbkiObjectId =
202 Catalog::FindDefinedSymbol('access/transam.h', $include_path,
203 'FirstGenbkiObjectId');
204 my $FirstUnpinnedObjectId =
205 Catalog::FindDefinedSymbol('access/transam.h', $include_path,
206 'FirstUnpinnedObjectId');
207 # Hash of next available OID, indexed by catalog name.
208 my %GenbkiNextOids;
209
210
211 # Fetch some special data that we will substitute into the output file.
212 # CAUTION: be wary about what symbols you substitute into the .bki file here!
213 # It's okay to substitute things that are expected to be really constant
214 # within a given Postgres release, such as fixed OIDs. Do not substitute
215 # anything that could depend on platform or configuration. (The right place
216 # to handle those sorts of things is in initdb.c's bootstrap_template1().)
217 my $C_COLLATION_OID =
218 Catalog::FindDefinedSymbolFromData($catalog_data{pg_collation},
219 'C_COLLATION_OID');
220
221
222 # Fill in pg_class.relnatts by looking at the referenced catalog's schema.
223 # This is ugly but there's no better place; Catalog::AddDefaultValues
224 # can't do it, for lack of easy access to the other catalog.
225 foreach my $row (@{ $catalog_data{pg_class} })
226 {
227 $row->{relnatts} = scalar(@{ $catalogs{ $row->{relname} }->{columns} });
228 }
229
230
231 # Build lookup tables.
232
233 # access method OID lookup
234 my %amoids;
235 foreach my $row (@{ $catalog_data{pg_am} })
236 {
237 $amoids{ $row->{amname} } = $row->{oid};
238 }
239
240 # role OID lookup
241 my %authidoids;
242 foreach my $row (@{ $catalog_data{pg_authid} })
243 {
244 $authidoids{ $row->{rolname} } = $row->{oid};
245 }
246
247 # class (relation) OID lookup (note this only covers bootstrap catalogs!)
248 my %classoids;
249 foreach my $row (@{ $catalog_data{pg_class} })
250 {
251 $classoids{ $row->{relname} } = $row->{oid};
252 }
253
254 # collation OID lookup
255 my %collationoids;
256 foreach my $row (@{ $catalog_data{pg_collation} })
257 {
258 $collationoids{ $row->{collname} } = $row->{oid};
259 }
260
261 # language OID lookup
262 my %langoids;
263 foreach my $row (@{ $catalog_data{pg_language} })
264 {
265 $langoids{ $row->{lanname} } = $row->{oid};
266 }
267
268 # namespace (schema) OID lookup
269 my %namespaceoids;
270 foreach my $row (@{ $catalog_data{pg_namespace} })
271 {
272 $namespaceoids{ $row->{nspname} } = $row->{oid};
273 }
274
275 # opclass OID lookup
276 my %opcoids;
277 foreach my $row (@{ $catalog_data{pg_opclass} })
278 {
279 # There is no unique name, so we need to combine access method
280 # and opclass name.
281 my $key = sprintf "%s/%s", $row->{opcmethod}, $row->{opcname};
282 $opcoids{$key} = $row->{oid};
283 }
284
285 # operator OID lookup
286 my %operoids;
287 foreach my $row (@{ $catalog_data{pg_operator} })
288 {
289 # There is no unique name, so we need to invent one that contains
290 # the relevant type names.
291 my $key = sprintf "%s(%s,%s)",
292 $row->{oprname}, $row->{oprleft}, $row->{oprright};
293 $operoids{$key} = $row->{oid};
294 }
295
296 # opfamily OID lookup
297 my %opfoids;
298 foreach my $row (@{ $catalog_data{pg_opfamily} })
299 {
300 # There is no unique name, so we need to combine access method
301 # and opfamily name.
302 my $key = sprintf "%s/%s", $row->{opfmethod}, $row->{opfname};
303 $opfoids{$key} = $row->{oid};
304 }
305
306 # procedure OID lookup
307 my %procoids;
308 foreach my $row (@{ $catalog_data{pg_proc} })
309 {
310 # Generate an entry under just the proname (corresponds to regproc lookup)
311 my $prokey = $row->{proname};
312 if (defined $procoids{$prokey})
313 {
314 $procoids{$prokey} = 'MULTIPLE';
315 }
316 else
317 {
318 $procoids{$prokey} = $row->{oid};
319 }
320
321 # Also generate an entry using proname(proargtypes). This is not quite
322 # identical to regprocedure lookup because we don't worry much about
323 # special SQL names for types etc; we just use the names in the source
324 # proargtypes field. These *should* be unique, but do a multiplicity
325 # check anyway.
326 $prokey .= '(' . join(',', split(/\s+/, $row->{proargtypes})) . ')';
327 if (defined $procoids{$prokey})
328 {
329 $procoids{$prokey} = 'MULTIPLE';
330 }
331 else
332 {
333 $procoids{$prokey} = $row->{oid};
334 }
335 }
336
337 # tablespace OID lookup
338 my %tablespaceoids;
339 foreach my $row (@{ $catalog_data{pg_tablespace} })
340 {
341 $tablespaceoids{ $row->{spcname} } = $row->{oid};
342 }
343
344 # text search configuration OID lookup
345 my %tsconfigoids;
346 foreach my $row (@{ $catalog_data{pg_ts_config} })
347 {
348 $tsconfigoids{ $row->{cfgname} } = $row->{oid};
349 }
350
351 # text search dictionary OID lookup
352 my %tsdictoids;
353 foreach my $row (@{ $catalog_data{pg_ts_dict} })
354 {
355 $tsdictoids{ $row->{dictname} } = $row->{oid};
356 }
357
358 # text search parser OID lookup
359 my %tsparseroids;
360 foreach my $row (@{ $catalog_data{pg_ts_parser} })
361 {
362 $tsparseroids{ $row->{prsname} } = $row->{oid};
363 }
364
365 # text search template OID lookup
366 my %tstemplateoids;
367 foreach my $row (@{ $catalog_data{pg_ts_template} })
368 {
369 $tstemplateoids{ $row->{tmplname} } = $row->{oid};
370 }
371
372 # type lookups
373 my %typeoids;
374 my %types;
375 foreach my $row (@{ $catalog_data{pg_type} })
376 {
377 # for OID macro substitutions
378 $typeoids{ $row->{typname} } = $row->{oid};
379
380 # for pg_attribute copies of pg_type values
381 $types{ $row->{typname} } = $row;
382 }
383
384 # Encoding identifier lookup. This uses the same replacement machinery
385 # as for OIDs, but we have to dig the values out of pg_wchar.h.
386 my %encids;
387
388 my $encfile = $include_path . 'mb/pg_wchar.h';
389 open(my $ef, '<', $encfile) || die "$encfile: $!";
390
391 # We're parsing an enum, so start with 0 and increment
392 # every time we find an enum member.
393 my $encid = 0;
394 my $collect_encodings = 0;
395 while (<$ef>)
396 {
397 if (/typedef\s+enum\s+pg_enc/)
398 {
399 $collect_encodings = 1;
400 next;
401 }
402
403 last if /_PG_LAST_ENCODING_/;
404
405 if ($collect_encodings and /^\s+(PG_\w+)/)
406 {
407 $encids{$1} = $encid;
408 $encid++;
409 }
410 }
411
412 close $ef;
413
414 # Map lookup name to the corresponding hash table.
415 my %lookup_kind = (
416 pg_am => \%amoids,
417 pg_authid => \%authidoids,
418 pg_class => \%classoids,
419 pg_collation => \%collationoids,
420 pg_language => \%langoids,
421 pg_namespace => \%namespaceoids,
422 pg_opclass => \%opcoids,
423 pg_operator => \%operoids,
424 pg_opfamily => \%opfoids,
425 pg_proc => \%procoids,
426 pg_tablespace => \%tablespaceoids,
427 pg_ts_config => \%tsconfigoids,
428 pg_ts_dict => \%tsdictoids,
429 pg_ts_parser => \%tsparseroids,
430 pg_ts_template => \%tstemplateoids,
431 pg_type => \%typeoids,
432 encoding => \%encids);
433
434
435 # Open temp files
436 my $tmpext = ".tmp$$";
437 my $bkifile = $output_path . 'postgres.bki';
438 open my $bki, '>', $bkifile . $tmpext
439 or die "can't open $bkifile$tmpext: $!";
440 my $schemafile = $output_path . 'schemapg.h';
441 open my $schemapg, '>', $schemafile . $tmpext
442 or die "can't open $schemafile$tmpext: $!";
443 my $fk_info_file = $output_path . 'system_fk_info.h';
444 open my $fk_info, '>', $fk_info_file . $tmpext
445 or die "can't open $fk_info_file$tmpext: $!";
446 my $constraints_file = $output_path . 'system_constraints.sql';
447 open my $constraints, '>', $constraints_file . $tmpext
448 or die "can't open $constraints_file$tmpext: $!";
449 my $syscache_ids_file = $output_path . 'syscache_ids.h';
450 open my $syscache_ids_fh, '>', $syscache_ids_file . $tmpext
451 or die "can't open $syscache_ids_file$tmpext: $!";
452 my $syscache_info_file = $output_path . 'syscache_info.h';
453 open my $syscache_info_fh, '>', $syscache_info_file . $tmpext
454 or die "can't open $syscache_info_file$tmpext: $!";
455
456 # Generate postgres.bki and pg_*_d.h headers.
457
458 # version marker for .bki file
459 print $bki "# PostgreSQL $major_version\n";
460
461 # vars to hold data needed for schemapg.h
462 my %schemapg_entries;
463 my @tables_needing_macros;
464
465 # produce output, one catalog at a time
466 foreach my $catname (@catnames)
467 {
468 my $catalog = $catalogs{$catname};
469
470 # Create one definition header with macro definitions for each catalog.
471 my $def_file = $output_path . $catname . '_d.h';
472 open my $def, '>', $def_file . $tmpext
473 or die "can't open $def_file$tmpext: $!";
474
475 print_boilerplate($def, "${catname}_d.h",
476 "Macro definitions for $catname");
477 printf $def <<EOM, uc $catname, uc $catname;
478 #ifndef %s_D_H
479 #define %s_D_H
480
481 EOM
482
483 printf $def "/* Macros related to the structure of $catname */\n\n";
484
485 # Emit OID macros for catalog's OID and rowtype OID, if wanted
486 printf $def "#define %s %s\n",
487 $catalog->{relation_oid_macro}, $catalog->{relation_oid}
488 if $catalog->{relation_oid_macro};
489 printf $def "#define %s %s\n",
490 $catalog->{rowtype_oid_macro}, $catalog->{rowtype_oid}
491 if $catalog->{rowtype_oid_macro};
492
493 # Likewise for macros for toast, index, and other OIDs
494 foreach my $toast (@{ $catalog->{toasting} })
495 {
496 printf $def "#define %s %s\n",
497 $toast->{toast_oid_macro}, $toast->{toast_oid}
498 if $toast->{toast_oid_macro};
499 printf $def "#define %s %s\n",
500 $toast->{toast_index_oid_macro}, $toast->{toast_index_oid}
501 if $toast->{toast_index_oid_macro};
502 }
503 foreach my $index (@{ $catalog->{indexing} })
504 {
505 printf $def "#define %s %s\n",
506 $index->{index_oid_macro}, $index->{index_oid}
507 if $index->{index_oid_macro};
508 }
509 foreach my $other (@{ $catalog->{other_oids} })
510 {
511 printf $def "#define %s %s\n",
512 $other->{other_name}, $other->{other_oid}
513 if $other->{other_name};
514 }
515
516 print $def "\n";
517
518 # .bki CREATE command for this catalog
519 print $bki "create $catname $catalog->{relation_oid}"
520 . $catalog->{shared_relation}
521 . $catalog->{bootstrap}
522 . $catalog->{rowtype_oid_clause};
523
524 my $first = 1;
525
526 print $bki "\n (\n";
527 my $schema = $catalog->{columns};
528 my %attnames;
529 my $attnum = 0;
530 foreach my $column (@$schema)
531 {
532 $attnum++;
533 my $attname = $column->{name};
534 my $atttype = $column->{type};
535
536 # Build hash of column names for use later
537 $attnames{$attname} = 1;
538
539 # Emit column definitions
540 if (!$first)
541 {
542 print $bki " ,\n";
543 }
544 $first = 0;
545
546 print $bki " $attname = $atttype";
547
548 if (defined $column->{forcenotnull})
549 {
550 print $bki " FORCE NOT NULL";
551 }
552 elsif (defined $column->{forcenull})
553 {
554 print $bki " FORCE NULL";
555 }
556
557 # Emit Anum_* constants
558 printf $def "#define Anum_%s_%s %s\n", $catname, $attname, $attnum;
559 }
560 print $bki "\n )\n";
561
562 # Emit Natts_* constant
563 print $def "\n#define Natts_$catname $attnum\n\n";
564
565 # Emit client code copied from source header
566 printf $def "/* Definitions copied from ${catname}.h */\n\n";
567 foreach my $line (@{ $catalog->{client_code} })
568 {
569 print $def $line;
570 }
571
572 # Open it, unless it's a bootstrap catalog (create bootstrap does this
573 # automatically)
574 if (!$catalog->{bootstrap})
575 {
576 print $bki "open $catname\n";
577 }
578
579 printf $def
580 "\n/* OID symbols for objects defined in ${catname}.dat */\n\n";
581
582 # For pg_attribute.h, we generate data entries ourselves.
583 if ($catname eq 'pg_attribute')
584 {
585 gen_pg_attribute($schema);
586 }
587
588 # Ordinary catalog with a data file
589 foreach my $row (@{ $catalog_data{$catname} })
590 {
591 my %bki_values = %$row;
592
593 # Complain about unrecognized keys; they are presumably misspelled
594 foreach my $key (keys %bki_values)
595 {
596 next
597 if $key eq "oid_symbol"
598 || $key eq "array_type_oid"
599 || $key eq "descr"
600 || $key eq "autogenerated"
601 || $key eq "line_number";
602 die sprintf "unrecognized field name \"%s\" in %s.dat line %s\n",
603 $key, $catname, $bki_values{line_number}
604 if (!exists($attnames{$key}));
605 }
606
607 # Perform required substitutions on fields
608 foreach my $column (@$schema)
609 {
610 my $attname = $column->{name};
611 my $atttype = $column->{type};
612
613 # Assign oid if oid column exists and no explicit assignment in row
614 if ($attname eq "oid" and not defined $bki_values{$attname})
615 {
616 $bki_values{$attname} = assign_next_oid($catname);
617 }
618
619 # Replace OID synonyms with OIDs per the appropriate lookup rule.
620 #
621 # If the column type is oidvector or _oid, we have to replace
622 # each element of the array as per the lookup rule.
623 if ($column->{lookup})
624 {
625 my $lookup = $lookup_kind{ $column->{lookup} };
626 my $lookup_opt = $column->{lookup_opt};
627 my @lookupnames;
628 my @lookupoids;
629
630 die "unrecognized BKI_LOOKUP type " . $column->{lookup}
631 if !defined($lookup);
632
633 if ($atttype eq 'oidvector')
634 {
635 @lookupnames = split /\s+/, $bki_values{$attname};
636 @lookupoids =
637 lookup_oids($lookup, $catname, $attname, $lookup_opt,
638 \%bki_values, @lookupnames);
639 $bki_values{$attname} = join(' ', @lookupoids);
640 }
641 elsif ($atttype eq '_oid')
642 {
643 if ($bki_values{$attname} ne '_null_')
644 {
645 $bki_values{$attname} =~ s/[{}]//g;
646 @lookupnames = split /,/, $bki_values{$attname};
647 @lookupoids =
648 lookup_oids($lookup, $catname, $attname,
649 $lookup_opt, \%bki_values, @lookupnames);
650 $bki_values{$attname} = sprintf "{%s}",
651 join(',', @lookupoids);
652 }
653 }
654 else
655 {
656 $lookupnames[0] = $bki_values{$attname};
657 @lookupoids =
658 lookup_oids($lookup, $catname, $attname, $lookup_opt,
659 \%bki_values, @lookupnames);
660 $bki_values{$attname} = $lookupoids[0];
661 }
662 }
663 }
664
665 # Special hack to generate OID symbols for pg_type entries
666 if ($catname eq 'pg_type')
667 {
668 die sprintf
669 "custom OID symbols are not allowed for pg_type entries: '%s'",
670 $bki_values{oid_symbol}
671 if defined $bki_values{oid_symbol};
672
673 my $symbol = form_pg_type_symbol($bki_values{typname});
674 $bki_values{oid_symbol} = $symbol
675 if defined $symbol;
676 }
677
678 # Write to postgres.bki
679 print_bki_insert(\%bki_values, $schema);
680
681 # Emit OID symbol
682 if (defined $bki_values{oid_symbol})
683 {
684 # OID symbols for builtin functions are handled automatically
685 # by utils/Gen_fmgrtab.pl
686 die sprintf
687 "custom OID symbols are not allowed for pg_proc entries: '%s'",
688 $bki_values{oid_symbol}
689 if $catname eq 'pg_proc';
690
691 printf $def "#define %s %s\n",
692 $bki_values{oid_symbol}, $bki_values{oid};
693 }
694 }
695
696 print $bki "close $catname\n";
697 printf $def "\n#endif\t\t\t\t\t\t\t/* %s_D_H */\n", uc $catname;
698
699 # Close and rename definition header
700 close $def;
701 Catalog::RenameTempFile($def_file, $tmpext);
702 }
703
704 # Any information needed for the BKI that is not contained in a pg_*.h header
705 # (i.e., not contained in a header with a CATALOG() statement) comes here
706
707 # Write out declare toast/index statements
708 foreach my $declaration (@toast_decls)
709 {
710 print $bki $declaration;
711 }
712
713 foreach my $declaration (@index_decls)
714 {
715 print $bki $declaration;
716 }
717
718 # last command in the BKI file: build the indexes declared above
719 print $bki "build indices\n";
720
721 # Now generate system_constraints.sql
722
723 foreach my $c (@system_constraints)
724 {
725 # leave blank lines to localize any bootstrap error messages better
726 print $constraints $c, "\n\n";
727 }
728
729 # Now generate schemapg.h
730
731 print_boilerplate($schemapg, "schemapg.h",
732 "Schema_pg_xxx macros for use by relcache.c");
733 print $schemapg <<EOM;
734 #ifndef SCHEMAPG_H
735 #define SCHEMAPG_H
736 EOM
737
738 # Emit schemapg declarations
739 foreach my $table_name (@tables_needing_macros)
740 {
741 print $schemapg "\n#define Schema_$table_name \\\n";
742 print $schemapg join ", \\\n", @{ $schemapg_entries{$table_name} };
743 print $schemapg "\n";
744 }
745
746 # Closing boilerplate for schemapg.h
747 print $schemapg "\n#endif\t\t\t\t\t\t\t/* SCHEMAPG_H */\n";
748
749 # Now generate system_fk_info.h
750
751 print_boilerplate($fk_info, "system_fk_info.h",
752 "Data about the foreign-key relationships in the system catalogs");
753 print $fk_info <<EOM;
754 #ifndef SYSTEM_FK_INFO_H
755 #define SYSTEM_FK_INFO_H
756
757 typedef struct SysFKRelationship
758 {
759 Oid fk_table; /* referencing catalog */
760 Oid pk_table; /* referenced catalog */
761 const char *fk_columns; /* referencing column name(s) */
762 const char *pk_columns; /* referenced column name(s) */
763 bool is_array; /* if true, last fk_column is an array */
764 bool is_opt; /* if true, fk_column can be zero */
765 } SysFKRelationship;
766
767 static const SysFKRelationship sys_fk_relationships[] = {
768 EOM
769
770 # Emit system_fk_info data
771 foreach my $catname (@catnames)
772 {
773 my $catalog = $catalogs{$catname};
774 foreach my $fkinfo (@{ $catalog->{foreign_keys} })
775 {
776 my $pktabname = $fkinfo->{pk_table};
777
778 # We use BKI_LOOKUP for encodings, but there's no real catalog there
779 next if $pktabname eq 'encoding';
780
781 printf $fk_info
782 "\t{ /* %s */ %s, /* %s */ %s, \"{%s}\", \"{%s}\", %s, %s},\n",
783 $catname, $catalog->{relation_oid},
784 $pktabname, $catalogs{$pktabname}->{relation_oid},
785 $fkinfo->{fk_cols},
786 $fkinfo->{pk_cols},
787 ($fkinfo->{is_array} ? "true" : "false"),
788 ($fkinfo->{is_opt} ? "true" : "false");
789 }
790 }
791
792 # Closing boilerplate for system_fk_info.h
793 print $fk_info "};\n\n#endif\t\t\t\t\t\t\t/* SYSTEM_FK_INFO_H */\n";
794
795 # Now generate syscache info
796
797 print_boilerplate($syscache_ids_fh, "syscache_ids.h", "SysCache identifiers");
798 print $syscache_ids_fh "enum SysCacheIdentifier
799 {
800 ";
801
802 print_boilerplate($syscache_info_fh, "syscache_info.h",
803 "SysCache definitions");
804 print $syscache_info_fh "\n";
805 foreach my $catname (sort keys %syscache_catalogs)
806 {
807 print $syscache_info_fh qq{#include "catalog/${catname}_d.h"\n};
808 }
809 print $syscache_info_fh "\n";
810 print $syscache_info_fh "static const struct cachedesc cacheinfo[] = {\n";
811
812 my $last_syscache;
813 foreach my $syscache (sort keys %syscaches)
814 {
815 print $syscache_ids_fh "\t$syscache,\n";
816 $last_syscache = $syscache;
817
818 print $syscache_info_fh "\t[$syscache] = {\n";
819 print $syscache_info_fh "\t\t", $syscaches{$syscache}{table_oid_macro},
820 ",\n";
821 print $syscache_info_fh "\t\t", $syscaches{$syscache}{index_oid_macro},
822 ",\n";
823 print $syscache_info_fh "\t\tKEY(", $syscaches{$syscache}{key}, "),\n";
824 print $syscache_info_fh "\t\t", $syscaches{$syscache}{nbuckets}, "\n";
825 print $syscache_info_fh "\t},\n";
826 }
827
828 print $syscache_ids_fh "};\n";
829 print $syscache_ids_fh "#define SysCacheSize ($last_syscache + 1)\n";
830
831 print $syscache_info_fh "};\n";
832
833 # We're done emitting data
834 close $bki;
835 close $schemapg;
836 close $fk_info;
837 close $constraints;
838 close $syscache_ids_fh;
839 close $syscache_info_fh;
840
841 # Finally, rename the completed files into place.
842 Catalog::RenameTempFile($bkifile, $tmpext);
843 Catalog::RenameTempFile($schemafile, $tmpext);
844 Catalog::RenameTempFile($fk_info_file, $tmpext);
845 Catalog::RenameTempFile($constraints_file, $tmpext);
846 Catalog::RenameTempFile($syscache_ids_file, $tmpext);
847 Catalog::RenameTempFile($syscache_info_file, $tmpext);
848
849 exit($num_errors != 0 ? 1 : 0);
850
851 #################### Subroutines ########################
852
853
854 # For each catalog marked as needing a schema macro, generate the
855 # per-user-attribute data to be incorporated into schemapg.h. Also, for
856 # bootstrap catalogs, emit pg_attribute entries into the .bki file
857 # for both user and system attributes.
858 sub gen_pg_attribute
859 {
860 my $schema = shift;
861
862 my @attnames;
863 foreach my $column (@$schema)
864 {
865 push @attnames, $column->{name};
866 }
867
868 foreach my $table_name (@catnames)
869 {
870 my $table = $catalogs{$table_name};
871
872 # Currently, all bootstrap catalogs also need schemapg.h
873 # entries, so skip if it isn't to be in schemapg.h.
874 next if !$table->{schema_macro};
875
876 $schemapg_entries{$table_name} = [];
877 push @tables_needing_macros, $table_name;
878
879 # Generate entries for user attributes.
880 my $attnum = 0;
881 my $priorfixedwidth = 1;
882 foreach my $attr (@{ $table->{columns} })
883 {
884 $attnum++;
885 my %row;
886 $row{attnum} = $attnum;
887 $row{attrelid} = $table->{relation_oid};
888
889 morph_row_for_pgattr(\%row, $schema, $attr, $priorfixedwidth);
890
891 # Update $priorfixedwidth --- must match morph_row_for_pgattr
892 $priorfixedwidth &=
893 ($row{attnotnull} eq 't'
894 && ($row{attlen} eq 'NAMEDATALEN' || $row{attlen} > 0));
895
896 # If it's bootstrapped, put an entry in postgres.bki.
897 print_bki_insert(\%row, $schema) if $table->{bootstrap};
898
899 # Store schemapg entries for later.
900 morph_row_for_schemapg(\%row, $schema);
901 push @{ $schemapg_entries{$table_name} },
902 sprintf "{ %s }",
903 join(', ', grep { defined $_ } @row{@attnames});
904 }
905
906 # Generate entries for system attributes.
907 # We only need postgres.bki entries, not schemapg.h entries.
908 if ($table->{bootstrap})
909 {
910 $attnum = 0;
911 my @SYS_ATTRS = (
912 { name => 'ctid', type => 'tid' },
913 { name => 'xmin', type => 'xid' },
914 { name => 'cmin', type => 'cid' },
915 { name => 'xmax', type => 'xid' },
916 { name => 'cmax', type => 'cid' },
917 { name => 'tableoid', type => 'oid' });
918 foreach my $attr (@SYS_ATTRS)
919 {
920 $attnum--;
921 my %row;
922 $row{attnum} = $attnum;
923 $row{attrelid} = $table->{relation_oid};
924
925 morph_row_for_pgattr(\%row, $schema, $attr, 1);
926 print_bki_insert(\%row, $schema);
927 }
928 }
929 }
930 return;
931 }
932
933 # Given $pgattr_schema (the pg_attribute schema for a catalog sufficient for
934 # AddDefaultValues), $attr (the description of a catalog row), and
935 # $priorfixedwidth (all prior columns are fixed-width and not null),
936 # modify the $row hashref for print_bki_insert. This includes setting data
937 # from the corresponding pg_type element and filling in any default values.
938 # Any value not handled here must be supplied by caller.
939 sub morph_row_for_pgattr
940 {
941 my ($row, $pgattr_schema, $attr, $priorfixedwidth) = @_;
942 my $attname = $attr->{name};
943 my $atttype = $attr->{type};
944
945 $row->{attname} = $attname;
946
947 # Copy the type data from pg_type, and add some type-dependent items
948 my $type = $types{$atttype};
949
950 $row->{atttypid} = $type->{oid};
951 $row->{attlen} = $type->{typlen};
952 $row->{attbyval} = $type->{typbyval};
953 $row->{attalign} = $type->{typalign};
954 $row->{attstorage} = $type->{typstorage};
955
956 # set attndims if it's an array type
957 $row->{attndims} = $type->{typcategory} eq 'A' ? '1' : '0';
958
959 # collation-aware catalog columns must use C collation
960 $row->{attcollation} =
961 $type->{typcollation} ne '0' ? $C_COLLATION_OID : 0;
962
963 if (defined $attr->{forcenotnull})
964 {
965 $row->{attnotnull} = 't';
966 }
967 elsif (defined $attr->{forcenull})
968 {
969 $row->{attnotnull} = 'f';
970 }
971 elsif ($priorfixedwidth)
972 {
973
974 # attnotnull will automatically be set if the type is
975 # fixed-width and prior columns are likewise fixed-width
976 # and NOT NULL --- compare DefineAttr in bootstrap.c.
977 # At this point the width of type name is still symbolic,
978 # so we need a special test.
979 $row->{attnotnull} =
980 $row->{attlen} eq 'NAMEDATALEN' ? 't'
981 : $row->{attlen} > 0 ? 't'
982 : 'f';
983 }
984 else
985 {
986 $row->{attnotnull} = 'f';
987 }
988
989 Catalog::AddDefaultValues($row, $pgattr_schema, 'pg_attribute');
990 return;
991 }
992
993 # Write an entry to postgres.bki.
994 sub print_bki_insert
995 {
996 my $row = shift;
997 my $schema = shift;
998
999 my @bki_values;
1000
1001 foreach my $column (@$schema)
1002 {
1003 my $attname = $column->{name};
1004 my $atttype = $column->{type};
1005 my $bki_value = $row->{$attname};
1006
1007 # Fold backslash-zero to empty string if it's the entire string,
1008 # since that represents a NUL char in C code.
1009 $bki_value = '' if $bki_value eq '\0';
1010
1011 # Handle single quotes by doubling them, because that's what the
1012 # bootstrap scanner requires. We do not process backslashes
1013 # specially; this allows escape-string-style backslash escapes
1014 # to be used in catalog data.
1015 $bki_value =~ s/'/''/g;
1016
1017 # Quote value if needed. We need not quote values that satisfy
1018 # the "id" pattern in bootscanner.l, currently "[-A-Za-z0-9_]+".
1019 $bki_value = sprintf("'%s'", $bki_value)
1020 if length($bki_value) == 0
1021 or $bki_value =~ /[^-A-Za-z0-9_]/;
1022
1023 push @bki_values, $bki_value;
1024 }
1025 printf $bki "insert ( %s )\n", join(' ', @bki_values);
1026 return;
1027 }
1028
1029 # Given a row reference, modify it so that it becomes a valid entry for
1030 # a catalog schema declaration in schemapg.h.
1031 #
1032 # The field values of a Schema_pg_xxx declaration are similar, but not
1033 # quite identical, to the corresponding values in postgres.bki.
1034 sub morph_row_for_schemapg
1035 {
1036 my $row = shift;
1037 my $pgattr_schema = shift;
1038
1039 foreach my $column (@$pgattr_schema)
1040 {
1041 my $attname = $column->{name};
1042 my $atttype = $column->{type};
1043
1044 # Some data types have special formatting rules.
1045 if ($atttype eq 'name')
1046 {
1047 # add {" ... "} quoting
1048 $row->{$attname} = sprintf(qq'{"%s"}', $row->{$attname});
1049 }
1050 elsif ($atttype eq 'char')
1051 {
1052 # Add single quotes
1053 $row->{$attname} = sprintf("'%s'", $row->{$attname});
1054 }
1055
1056 # Expand booleans from 'f'/'t' to 'false'/'true'.
1057 # Some values might be other macros (eg FLOAT8PASSBYVAL),
1058 # don't change.
1059 elsif ($atttype eq 'bool')
1060 {
1061 $row->{$attname} = 'true' if $row->{$attname} eq 't';
1062 $row->{$attname} = 'false' if $row->{$attname} eq 'f';
1063 }
1064
1065 # We don't emit initializers for the variable length fields at all.
1066 # Only the fixed-size portions of the descriptors are ever used.
1067 delete $row->{$attname} if $column->{is_varlen};
1068 }
1069 return;
1070 }
1071
1072 # Perform OID lookups on an array of OID names.
1073 # If we don't have a unique value to substitute, warn and
1074 # leave the entry unchanged.
1075 # (We don't exit right away so that we can detect multiple problems
1076 # within this genbki.pl run.)
1077 sub lookup_oids
1078 {
1079 my ($lookup, $catname, $attname, $lookup_opt, $bki_values, @lookupnames)
1080 = @_;
1081
1082 my @lookupoids;
1083 foreach my $lookupname (@lookupnames)
1084 {
1085 my $lookupoid = $lookup->{$lookupname};
1086 if (defined($lookupoid) and $lookupoid ne 'MULTIPLE')
1087 {
1088 push @lookupoids, $lookupoid;
1089 }
1090 else
1091 {
1092 push @lookupoids, $lookupname;
1093 if ($lookupname eq '-' or $lookupname eq '0')
1094 {
1095 if (!$lookup_opt)
1096 {
1097 warn sprintf
1098 "invalid zero OID reference in %s.dat field %s line %s\n",
1099 $catname, $attname, $bki_values->{line_number};
1100 $num_errors++;
1101 }
1102 }
1103 else
1104 {
1105 warn sprintf
1106 "unresolved OID reference \"%s\" in %s.dat field %s line %s\n",
1107 $lookupname, $catname, $attname, $bki_values->{line_number};
1108 $num_errors++;
1109 }
1110 }
1111 }
1112 return @lookupoids;
1113 }
1114
1115 # Determine canonical pg_type OID #define symbol from the type name.
1116 sub form_pg_type_symbol
1117 {
1118 my $typename = shift;
1119
1120 # Skip for rowtypes of bootstrap catalogs, since they have their
1121 # own naming convention defined elsewhere.
1122 return
1123 if $typename eq 'pg_type'
1124 or $typename eq 'pg_proc'
1125 or $typename eq 'pg_attribute'
1126 or $typename eq 'pg_class';
1127
1128 # Transform like so:
1129 # foo_bar -> FOO_BAROID
1130 # _foo_bar -> FOO_BARARRAYOID
1131 $typename =~ /(_)?(.+)/;
1132 my $arraystr = $1 ? 'ARRAY' : '';
1133 my $name = uc $2;
1134 return $name . $arraystr . 'OID';
1135 }
1136
1137 # Assign an unused OID within the specified catalog.
1138 sub assign_next_oid
1139 {
1140 my $catname = shift;
1141
1142 # Initialize, if no previous request for this catalog.
1143 $GenbkiNextOids{$catname} = $FirstGenbkiObjectId
1144 if !defined($GenbkiNextOids{$catname});
1145
1146 my $result = $GenbkiNextOids{$catname}++;
1147
1148 # Check that we didn't overrun available OIDs
1149 die
1150 "genbki OID counter for $catname reached $result, overrunning FirstUnpinnedObjectId\n"
1151 if $result >= $FirstUnpinnedObjectId;
1152
1153 return $result;
1154 }
1155
1156 sub print_boilerplate
1157 {
1158 my ($fh, $fname, $descr) = @_;
1159 printf $fh <<EOM, $fname, $descr;
1160 /*-------------------------------------------------------------------------
1161 *
1162 * %s
1163 * %s
1164 *
1165 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
1166 * Portions Copyright (c) 1994, Regents of the University of California
1167 *
1168 * NOTES
1169 * ******************************
1170 * *** DO NOT EDIT THIS FILE! ***
1171 * ******************************
1172 *
1173 * It has been GENERATED by src/backend/catalog/genbki.pl
1174 *
1175 *-------------------------------------------------------------------------
1176 */
1177 EOM
1178 }
1179
1180 sub usage
1181 {
1182 die <<EOM;
1183 Usage: perl -I [directory of Catalog.pm] genbki.pl [--output/-o <path>] [--include-path/-i <path>] header...
1184
1185 Options:
1186 --output Output directory (default '.')
1187 --set-version PostgreSQL version number for initdb cross-check
1188 --include-path Include path in source tree
1189
1190 genbki.pl generates postgres.bki and symbol definition
1191 headers from specially formatted header files and .dat
1192 files. postgres.bki is used to initialize the
1193 postgres template database.
1194
1195 Report bugs to <pgsql-bugs\@lists.postgresql.org>.
1196 EOM
1197 }