| 1 |
#! /usr/bin/perl |
| 2 |
#------------------------------------------------------------------------- |
| 3 |
# |
| 4 |
# Gen_fmgrtab.pl |
| 5 |
# Perl script that generates fmgroids.h, fmgrprotos.h, and fmgrtab.c |
| 6 |
# from pg_proc.dat |
| 7 |
# |
| 8 |
# Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 9 |
# Portions Copyright (c) 1994, Regents of the University of California |
| 10 |
# |
| 11 |
# |
| 12 |
# IDENTIFICATION |
| 13 |
# src/backend/utils/Gen_fmgrtab.pl |
| 14 |
# |
| 15 |
#------------------------------------------------------------------------- |
| 16 |
|
| 17 |
use Catalog; |
| 18 |
|
| 19 |
use strict; |
| 20 |
use warnings FATAL => 'all'; |
| 21 |
use Getopt::Long; |
| 22 |
|
| 23 |
my $output_path = ''; |
| 24 |
my $include_path; |
| 25 |
|
| 26 |
GetOptions( |
| 27 |
'output:s' => \$output_path, |
| 28 |
'include-path:s' => \$include_path) || usage(); |
| 29 |
|
| 30 |
# Make sure output_path ends in a slash. |
| 31 |
if ($output_path ne '' && substr($output_path, -1) ne '/') |
| 32 |
{ |
| 33 |
$output_path .= '/'; |
| 34 |
} |
| 35 |
|
| 36 |
# Sanity check arguments. |
| 37 |
die "No input files.\n" unless @ARGV; |
| 38 |
die "--include-path must be specified.\n" unless $include_path; |
| 39 |
|
| 40 |
# Read all the input files into internal data structures. |
| 41 |
# Note: We pass data file names as arguments and then look for matching |
| 42 |
# headers to parse the schema from. This is backwards from genbki.pl, |
| 43 |
# but the Makefile dependencies look more sensible this way. |
| 44 |
# We currently only need pg_proc, but retain the possibility of reading |
| 45 |
# more than one data file. |
| 46 |
my %catalogs; |
| 47 |
my %catalog_data; |
| 48 |
foreach my $datfile (@ARGV) |
| 49 |
{ |
| 50 |
$datfile =~ /(.+)\.dat$/ |
| 51 |
or die "Input files need to be data (.dat) files.\n"; |
| 52 |
|
| 53 |
my $header = "$1.h"; |
| 54 |
die "There in no header file corresponding to $datfile" |
| 55 |
if !-e $header; |
| 56 |
|
| 57 |
my $catalog = Catalog::ParseHeader($header); |
| 58 |
my $catname = $catalog->{catname}; |
| 59 |
my $schema = $catalog->{columns}; |
| 60 |
|
| 61 |
$catalogs{$catname} = $catalog; |
| 62 |
$catalog_data{$catname} = Catalog::ParseData($datfile, $schema, 0); |
| 63 |
} |
| 64 |
|
| 65 |
# Collect certain fields from pg_proc.dat. |
| 66 |
my @fmgr = (); |
| 67 |
my %proname_counts; |
| 68 |
|
| 69 |
foreach my $row (@{ $catalog_data{pg_proc} }) |
| 70 |
{ |
| 71 |
my %bki_values = %$row; |
| 72 |
|
| 73 |
push @fmgr, |
| 74 |
{ |
| 75 |
oid => $bki_values{oid}, |
| 76 |
name => $bki_values{proname}, |
| 77 |
lang => $bki_values{prolang}, |
| 78 |
kind => $bki_values{prokind}, |
| 79 |
strict => $bki_values{proisstrict}, |
| 80 |
retset => $bki_values{proretset}, |
| 81 |
nargs => $bki_values{pronargs}, |
| 82 |
args => $bki_values{proargtypes}, |
| 83 |
prosrc => $bki_values{prosrc}, |
| 84 |
}; |
| 85 |
|
| 86 |
# Count so that we can detect overloaded pronames. |
| 87 |
$proname_counts{ $bki_values{proname} }++; |
| 88 |
} |
| 89 |
|
| 90 |
# Emit headers for both files |
| 91 |
my $tmpext = ".tmp$$"; |
| 92 |
my $oidsfile = $output_path . 'fmgroids.h'; |
| 93 |
my $protosfile = $output_path . 'fmgrprotos.h'; |
| 94 |
my $tabfile = $output_path . 'fmgrtab.c'; |
| 95 |
|
| 96 |
open my $ofh, '>', $oidsfile . $tmpext |
| 97 |
or die "Could not open $oidsfile$tmpext: $!"; |
| 98 |
open my $pfh, '>', $protosfile . $tmpext |
| 99 |
or die "Could not open $protosfile$tmpext: $!"; |
| 100 |
open my $tfh, '>', $tabfile . $tmpext |
| 101 |
or die "Could not open $tabfile$tmpext: $!"; |
| 102 |
|
| 103 |
print $ofh <<OFH; |
| 104 |
/*------------------------------------------------------------------------- |
| 105 |
* |
| 106 |
* fmgroids.h |
| 107 |
* Macros that define the OIDs of built-in functions. |
| 108 |
* |
| 109 |
* These macros can be used to avoid a catalog lookup when a specific |
| 110 |
* fmgr-callable function needs to be referenced. |
| 111 |
* |
| 112 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 113 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 114 |
* |
| 115 |
* NOTES |
| 116 |
* ****************************** |
| 117 |
* *** DO NOT EDIT THIS FILE! *** |
| 118 |
* ****************************** |
| 119 |
* |
| 120 |
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl |
| 121 |
* |
| 122 |
*------------------------------------------------------------------------- |
| 123 |
*/ |
| 124 |
#ifndef FMGROIDS_H |
| 125 |
#define FMGROIDS_H |
| 126 |
|
| 127 |
/* |
| 128 |
* Constant macros for the OIDs of entries in pg_proc. |
| 129 |
* |
| 130 |
* F_XXX macros are named after the proname field; if that is not unique, |
| 131 |
* we append the proargtypes field, replacing spaces with underscores. |
| 132 |
* For example, we have F_OIDEQ because that proname is unique, but |
| 133 |
* F_POW_FLOAT8_FLOAT8 (among others) because that proname is not. |
| 134 |
*/ |
| 135 |
OFH |
| 136 |
|
| 137 |
print $pfh <<PFH; |
| 138 |
/*------------------------------------------------------------------------- |
| 139 |
* |
| 140 |
* fmgrprotos.h |
| 141 |
* Prototypes for built-in functions. |
| 142 |
* |
| 143 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 144 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 145 |
* |
| 146 |
* NOTES |
| 147 |
* ****************************** |
| 148 |
* *** DO NOT EDIT THIS FILE! *** |
| 149 |
* ****************************** |
| 150 |
* |
| 151 |
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl |
| 152 |
* |
| 153 |
*------------------------------------------------------------------------- |
| 154 |
*/ |
| 155 |
|
| 156 |
#ifndef FMGRPROTOS_H |
| 157 |
#define FMGRPROTOS_H |
| 158 |
|
| 159 |
#include "fmgr.h" |
| 160 |
|
| 161 |
PFH |
| 162 |
|
| 163 |
print $tfh <<TFH; |
| 164 |
/*------------------------------------------------------------------------- |
| 165 |
* |
| 166 |
* fmgrtab.c |
| 167 |
* The function manager's table of internal functions. |
| 168 |
* |
| 169 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 170 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 171 |
* |
| 172 |
* NOTES |
| 173 |
* |
| 174 |
* ****************************** |
| 175 |
* *** DO NOT EDIT THIS FILE! *** |
| 176 |
* ****************************** |
| 177 |
* |
| 178 |
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl |
| 179 |
* |
| 180 |
*------------------------------------------------------------------------- |
| 181 |
*/ |
| 182 |
|
| 183 |
#include "postgres.h" |
| 184 |
|
| 185 |
#include "utils/fmgrtab.h" |
| 186 |
#include "utils/fmgrprotos.h" |
| 187 |
|
| 188 |
TFH |
| 189 |
|
| 190 |
# Emit fmgroids.h and fmgrprotos.h entries in OID order. |
| 191 |
my %seenit; |
| 192 |
foreach my $s (sort { $a->{oid} <=> $b->{oid} } @fmgr) |
| 193 |
{ |
| 194 |
my $sqlname = $s->{name}; |
| 195 |
$sqlname .= "_" . $s->{args} if ($proname_counts{ $s->{name} } > 1); |
| 196 |
$sqlname =~ s/\s+/_/g; |
| 197 |
print $ofh "#define F_" . uc $sqlname . " $s->{oid}\n"; |
| 198 |
# We want only one extern per internal-language, non-aggregate function |
| 199 |
if ( $s->{lang} eq 'internal' |
| 200 |
&& $s->{kind} ne 'a' |
| 201 |
&& !$seenit{ $s->{prosrc} }) |
| 202 |
{ |
| 203 |
$seenit{ $s->{prosrc} } = 1; |
| 204 |
print $pfh "extern Datum $s->{prosrc}(PG_FUNCTION_ARGS);\n"; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
# Create the fmgr_builtins table, collect data for fmgr_builtin_oid_index |
| 209 |
print $tfh "\nconst FmgrBuiltin fmgr_builtins[] = {\n"; |
| 210 |
my %bmap; |
| 211 |
$bmap{'t'} = 'true'; |
| 212 |
$bmap{'f'} = 'false'; |
| 213 |
my @fmgr_builtin_oid_index; |
| 214 |
my $last_builtin_oid = 0; |
| 215 |
my $fmgr_count = 0; |
| 216 |
|
| 217 |
foreach my $s (sort { $a->{oid} <=> $b->{oid} } @fmgr) |
| 218 |
{ |
| 219 |
next if $s->{lang} ne 'internal'; |
| 220 |
# We do not need entries for aggregate functions |
| 221 |
next if $s->{kind} eq 'a'; |
| 222 |
|
| 223 |
print $tfh ",\n" if ($fmgr_count > 0); |
| 224 |
print $tfh |
| 225 |
" { $s->{oid}, $s->{nargs}, $bmap{$s->{strict}}, $bmap{$s->{retset}}, \"$s->{prosrc}\", $s->{prosrc} }"; |
| 226 |
|
| 227 |
$fmgr_builtin_oid_index[ $s->{oid} ] = $fmgr_count++; |
| 228 |
$last_builtin_oid = $s->{oid}; |
| 229 |
} |
| 230 |
print $tfh "\n};\n"; |
| 231 |
|
| 232 |
printf $tfh qq| |
| 233 |
const int fmgr_nbuiltins = (sizeof(fmgr_builtins) / sizeof(FmgrBuiltin)); |
| 234 |
|
| 235 |
const Oid fmgr_last_builtin_oid = %u; |
| 236 |
|, $last_builtin_oid; |
| 237 |
|
| 238 |
|
| 239 |
# Create fmgr_builtin_oid_index table. |
| 240 |
printf $tfh qq| |
| 241 |
const uint16 fmgr_builtin_oid_index[%u] = { |
| 242 |
|, $last_builtin_oid + 1; |
| 243 |
|
| 244 |
for (my $i = 0; $i <= $last_builtin_oid; $i++) |
| 245 |
{ |
| 246 |
my $oid = $fmgr_builtin_oid_index[$i]; |
| 247 |
|
| 248 |
# fmgr_builtin_oid_index is sparse, map nonexistent functions to |
| 249 |
# InvalidOidBuiltinMapping |
| 250 |
if (not defined $oid) |
| 251 |
{ |
| 252 |
$oid = 'InvalidOidBuiltinMapping'; |
| 253 |
} |
| 254 |
|
| 255 |
if ($i == $last_builtin_oid) |
| 256 |
{ |
| 257 |
print $tfh " $oid\n"; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
print $tfh " $oid,\n"; |
| 262 |
} |
| 263 |
} |
| 264 |
print $tfh "};\n"; |
| 265 |
|
| 266 |
|
| 267 |
# And add the file footers. |
| 268 |
print $ofh "\n#endif\t\t\t\t\t\t\t/* FMGROIDS_H */\n"; |
| 269 |
print $pfh "\n#endif\t\t\t\t\t\t\t/* FMGRPROTOS_H */\n"; |
| 270 |
|
| 271 |
close($ofh); |
| 272 |
close($pfh); |
| 273 |
close($tfh); |
| 274 |
|
| 275 |
# Finally, rename the completed files into place. |
| 276 |
Catalog::RenameTempFile($oidsfile, $tmpext); |
| 277 |
Catalog::RenameTempFile($protosfile, $tmpext); |
| 278 |
Catalog::RenameTempFile($tabfile, $tmpext); |
| 279 |
|
| 280 |
sub usage |
| 281 |
{ |
| 282 |
die <<EOM; |
| 283 |
Usage: perl -I [directory of Catalog.pm] Gen_fmgrtab.pl [--include-path/-i <path>] [path to pg_proc.dat] |
| 284 |
|
| 285 |
Options: |
| 286 |
--output Output directory (default '.') |
| 287 |
--include-path Include path in source tree |
| 288 |
|
| 289 |
Gen_fmgrtab.pl generates fmgroids.h, fmgrprotos.h, and fmgrtab.c from |
| 290 |
pg_proc.dat |
| 291 |
|
| 292 |
Report bugs to <pgsql-bugs\@lists.postgresql.org>. |
| 293 |
EOM |
| 294 |
} |
| 295 |
|
| 296 |
exit 0; |