2008-06-23 19:54:30 +02:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gen_fmgrtab.pl
|
2017-12-21 23:07:32 +01:00
|
|
|
# Perl script that generates fmgroids.h, fmgrprotos.h, and fmgrtab.c
|
|
|
|
# from pg_proc.h
|
2008-06-23 19:54:30 +02:00
|
|
|
#
|
2018-01-03 05:30:12 +01:00
|
|
|
# Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2008-06-23 19:54:30 +02:00
|
|
|
# Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
# src/backend/utils/Gen_fmgrtab.pl
|
2008-06-23 19:54:30 +02:00
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
2010-01-05 02:06:57 +01:00
|
|
|
use Catalog;
|
|
|
|
|
2008-06-23 19:54:30 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# Collect arguments
|
2012-07-05 03:47:49 +02:00
|
|
|
my $infile; # pg_proc.h
|
2010-01-05 02:06:57 +01:00
|
|
|
my $output_path = '';
|
2017-10-04 09:22:38 +02:00
|
|
|
my @include_path;
|
|
|
|
|
2010-01-05 02:06:57 +01:00
|
|
|
while (@ARGV)
|
2008-06-23 19:54:30 +02:00
|
|
|
{
|
2012-07-05 03:47:49 +02:00
|
|
|
my $arg = shift @ARGV;
|
|
|
|
if ($arg !~ /^-/)
|
|
|
|
{
|
|
|
|
$infile = $arg;
|
|
|
|
}
|
|
|
|
elsif ($arg =~ /^-o/)
|
|
|
|
{
|
|
|
|
$output_path = length($arg) > 2 ? substr($arg, 2) : shift @ARGV;
|
|
|
|
}
|
2017-10-04 09:22:38 +02:00
|
|
|
elsif ($arg =~ /^-I/)
|
|
|
|
{
|
|
|
|
push @include_path, length($arg) > 2 ? substr($arg, 2) : shift @ARGV;
|
|
|
|
}
|
2012-07-05 03:47:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
2008-06-23 19:54:30 +02:00
|
|
|
}
|
|
|
|
|
2010-01-05 02:06:57 +01:00
|
|
|
# Make sure output_path ends in a slash.
|
|
|
|
if ($output_path ne '' && substr($output_path, -1) ne '/')
|
|
|
|
{
|
2012-07-05 03:47:49 +02:00
|
|
|
$output_path .= '/';
|
2010-01-05 02:06:57 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 09:22:38 +02:00
|
|
|
# Sanity check arguments.
|
|
|
|
die "No input files.\n" if !$infile;
|
|
|
|
die "No include path; you must specify -I at least once.\n" if !@include_path;
|
|
|
|
|
|
|
|
my $FirstBootstrapObjectId =
|
|
|
|
Catalog::FindDefinedSymbol('access/transam.h', \@include_path, 'FirstBootstrapObjectId');
|
2017-12-21 23:07:32 +01:00
|
|
|
my $INTERNALlanguageId =
|
|
|
|
Catalog::FindDefinedSymbol('catalog/pg_language.h', \@include_path, 'INTERNALlanguageId');
|
2017-10-04 09:22:38 +02:00
|
|
|
|
2010-01-05 02:06:57 +01:00
|
|
|
# Read all the data from the include/catalog files.
|
|
|
|
my $catalogs = Catalog::Catalogs($infile);
|
|
|
|
|
|
|
|
# Collect the raw data from pg_proc.h.
|
2008-06-23 19:54:30 +02:00
|
|
|
my @fmgr = ();
|
2010-01-05 02:06:57 +01:00
|
|
|
my @attnames;
|
2012-07-05 03:47:49 +02:00
|
|
|
foreach my $column (@{ $catalogs->{pg_proc}->{columns} })
|
2010-01-05 02:06:57 +01:00
|
|
|
{
|
2015-02-21 22:25:49 +01:00
|
|
|
push @attnames, $column->{name};
|
2010-01-05 02:06:57 +01:00
|
|
|
}
|
2008-06-23 19:54:30 +02:00
|
|
|
|
2010-01-05 02:06:57 +01:00
|
|
|
my $data = $catalogs->{pg_proc}->{data};
|
|
|
|
foreach my $row (@$data)
|
2008-06-23 19:54:30 +02:00
|
|
|
{
|
2017-05-18 01:01:23 +02:00
|
|
|
|
Move bootstrap-time lookup of regproc OIDs into genbki.pl.
Formerly, the bootstrap backend looked up the OIDs corresponding to
names in regproc catalog entries using brute-force searches of pg_proc.
It was somewhat remarkable that that worked at all, since it was used
while populating other pretty-fundamental catalogs like pg_operator.
And it was also quite slow, and getting slower as pg_proc gets bigger.
This patch moves the lookup work into genbki.pl, so that the values in
postgres.bki for regproc columns are always numeric OIDs, an option
that regprocin() already supported. Perl isn't the world's speediest
language, so this about doubles the time needed to run genbki.pl (from
0.3 to 0.6 sec on my machine). But we only do that at most once per
build. The time needed to run initdb drops significantly --- on my
machine, initdb --no-sync goes from 1.8 to 1.3 seconds. So this is
a small net win even for just one initdb per build, and it becomes
quite a nice win for test sequences requiring many initdb runs.
Strip out the now-dead code for brute-force catalog searching in
regprocin. We'd also cargo-culted similar logic into regoperin
and some (not all) of the other reg*in functions. That is all
dead code too since we currently have no need to load such values
during bootstrap. I removed it all, reasoning that if we ever
need such functionality it'd be much better to do it in a similar
way to this patch.
There might be some simplifications possible in the backend now that
regprocin doesn't require doing catalog reads so early in bootstrap.
I've not looked into that, though.
Andreas Karlsson, with some small adjustments by me
Discussion: https://postgr.es/m/30896.1492006367@sss.pgh.pa.us
2017-04-13 18:07:47 +02:00
|
|
|
# Split line into tokens without interpreting their meaning.
|
|
|
|
my %bki_values;
|
|
|
|
@bki_values{@attnames} = Catalog::SplitDataLine($row->{bki_values});
|
2012-07-05 03:47:49 +02:00
|
|
|
|
|
|
|
# Select out just the rows for internal-language procedures.
|
2017-12-21 23:07:32 +01:00
|
|
|
next if $bki_values{prolang} ne $INTERNALlanguageId;
|
2012-07-05 03:47:49 +02:00
|
|
|
|
|
|
|
push @fmgr,
|
|
|
|
{ oid => $row->{oid},
|
Move bootstrap-time lookup of regproc OIDs into genbki.pl.
Formerly, the bootstrap backend looked up the OIDs corresponding to
names in regproc catalog entries using brute-force searches of pg_proc.
It was somewhat remarkable that that worked at all, since it was used
while populating other pretty-fundamental catalogs like pg_operator.
And it was also quite slow, and getting slower as pg_proc gets bigger.
This patch moves the lookup work into genbki.pl, so that the values in
postgres.bki for regproc columns are always numeric OIDs, an option
that regprocin() already supported. Perl isn't the world's speediest
language, so this about doubles the time needed to run genbki.pl (from
0.3 to 0.6 sec on my machine). But we only do that at most once per
build. The time needed to run initdb drops significantly --- on my
machine, initdb --no-sync goes from 1.8 to 1.3 seconds. So this is
a small net win even for just one initdb per build, and it becomes
quite a nice win for test sequences requiring many initdb runs.
Strip out the now-dead code for brute-force catalog searching in
regprocin. We'd also cargo-culted similar logic into regoperin
and some (not all) of the other reg*in functions. That is all
dead code too since we currently have no need to load such values
during bootstrap. I removed it all, reasoning that if we ever
need such functionality it'd be much better to do it in a similar
way to this patch.
There might be some simplifications possible in the backend now that
regprocin doesn't require doing catalog reads so early in bootstrap.
I've not looked into that, though.
Andreas Karlsson, with some small adjustments by me
Discussion: https://postgr.es/m/30896.1492006367@sss.pgh.pa.us
2017-04-13 18:07:47 +02:00
|
|
|
strict => $bki_values{proisstrict},
|
|
|
|
retset => $bki_values{proretset},
|
|
|
|
nargs => $bki_values{pronargs},
|
|
|
|
prosrc => $bki_values{prosrc}, };
|
2008-06-23 19:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Emit headers for both files
|
2017-05-18 01:01:23 +02:00
|
|
|
my $tmpext = ".tmp$$";
|
|
|
|
my $oidsfile = $output_path . 'fmgroids.h';
|
2016-12-28 18:00:00 +01:00
|
|
|
my $protosfile = $output_path . 'fmgrprotos.h';
|
2017-05-18 01:01:23 +02:00
|
|
|
my $tabfile = $output_path . 'fmgrtab.c';
|
2010-01-05 21:23:32 +01:00
|
|
|
|
2017-05-18 01:01:23 +02:00
|
|
|
open my $ofh, '>', $oidsfile . $tmpext
|
|
|
|
or die "Could not open $oidsfile$tmpext: $!";
|
|
|
|
open my $pfh, '>', $protosfile . $tmpext
|
|
|
|
or die "Could not open $protosfile$tmpext: $!";
|
|
|
|
open my $tfh, '>', $tabfile . $tmpext
|
|
|
|
or die "Could not open $tabfile$tmpext: $!";
|
2010-01-05 21:23:32 +01:00
|
|
|
|
2017-03-27 04:24:13 +02:00
|
|
|
print $ofh
|
2008-06-23 19:54:30 +02:00
|
|
|
qq|/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fmgroids.h
|
|
|
|
* Macros that define the OIDs of built-in functions.
|
|
|
|
*
|
|
|
|
* These macros can be used to avoid a catalog lookup when a specific
|
|
|
|
* fmgr-callable function needs to be referenced.
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2008-06-23 19:54:30 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* ******************************
|
|
|
|
* *** DO NOT EDIT THIS FILE! ***
|
|
|
|
* ******************************
|
|
|
|
*
|
Avoid putting build-location-dependent strings into generated files.
Various Perl scripts we use to generate files were in the habit of
printing things like "generated by $0" into their output files.
That looks like a fine idea at first glance, but it results in
non-reproducible output, because in VPATH builds $0 won't be just
the name of the script file, but a full path for it. We'd prefer
that you get identical results whether using VPATH or not, so this
is a bad thing.
Some of these places also printed their input file name(s), causing
an additional hazard of the same type.
Hence, establish a policy that thou shalt not print $0, nor input file
pathnames, into output files (they're still allowed in error messages,
though). Instead just write the script name verbatim. While we are at
it, we can make these annotations more useful by giving the script's
full relative path name within the PG source tree, eg instead of
Gen_fmgrtab.pl let's print src/backend/utils/Gen_fmgrtab.pl.
Not all of the changes made here actually affect any files shipped
in finished tarballs today, but it seems best to apply the policy
everyplace so that nobody copies unsafe code into places where it
could matter.
Christoph Berg and Tom Lane
Discussion: https://postgr.es/m/20171215102223.GB31812@msg.df7cb.de
2017-12-21 16:56:57 +01:00
|
|
|
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl
|
2008-06-23 19:54:30 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef FMGROIDS_H
|
|
|
|
#define FMGROIDS_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constant macros for the OIDs of entries in pg_proc.
|
|
|
|
*
|
|
|
|
* NOTE: macros are named after the prosrc value, ie the actual C name
|
|
|
|
* of the implementing function, not the proname which may be overloaded.
|
|
|
|
* For example, we want to be able to assign different macro names to both
|
|
|
|
* char_text() and name_text() even though these both appear with proname
|
|
|
|
* 'text'. If the same C function appears in more than one pg_proc entry,
|
|
|
|
* its equivalent macro will be defined with the lowest OID among those
|
|
|
|
* entries.
|
|
|
|
*/
|
|
|
|
|;
|
|
|
|
|
2017-03-27 04:24:13 +02:00
|
|
|
print $pfh
|
2016-12-28 18:00:00 +01:00
|
|
|
qq|/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fmgrprotos.h
|
|
|
|
* Prototypes for built-in functions.
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2016-12-28 18:00:00 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* ******************************
|
|
|
|
* *** DO NOT EDIT THIS FILE! ***
|
|
|
|
* ******************************
|
|
|
|
*
|
Avoid putting build-location-dependent strings into generated files.
Various Perl scripts we use to generate files were in the habit of
printing things like "generated by $0" into their output files.
That looks like a fine idea at first glance, but it results in
non-reproducible output, because in VPATH builds $0 won't be just
the name of the script file, but a full path for it. We'd prefer
that you get identical results whether using VPATH or not, so this
is a bad thing.
Some of these places also printed their input file name(s), causing
an additional hazard of the same type.
Hence, establish a policy that thou shalt not print $0, nor input file
pathnames, into output files (they're still allowed in error messages,
though). Instead just write the script name verbatim. While we are at
it, we can make these annotations more useful by giving the script's
full relative path name within the PG source tree, eg instead of
Gen_fmgrtab.pl let's print src/backend/utils/Gen_fmgrtab.pl.
Not all of the changes made here actually affect any files shipped
in finished tarballs today, but it seems best to apply the policy
everyplace so that nobody copies unsafe code into places where it
could matter.
Christoph Berg and Tom Lane
Discussion: https://postgr.es/m/20171215102223.GB31812@msg.df7cb.de
2017-12-21 16:56:57 +01:00
|
|
|
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl
|
2016-12-28 18:00:00 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FMGRPROTOS_H
|
|
|
|
#define FMGRPROTOS_H
|
|
|
|
|
|
|
|
#include "fmgr.h"
|
|
|
|
|
|
|
|
|;
|
|
|
|
|
2017-03-27 04:24:13 +02:00
|
|
|
print $tfh
|
2008-06-23 19:54:30 +02:00
|
|
|
qq|/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fmgrtab.c
|
|
|
|
* The function manager's table of internal functions.
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2008-06-23 19:54:30 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
*
|
|
|
|
* ******************************
|
|
|
|
* *** DO NOT EDIT THIS FILE! ***
|
|
|
|
* ******************************
|
|
|
|
*
|
Avoid putting build-location-dependent strings into generated files.
Various Perl scripts we use to generate files were in the habit of
printing things like "generated by $0" into their output files.
That looks like a fine idea at first glance, but it results in
non-reproducible output, because in VPATH builds $0 won't be just
the name of the script file, but a full path for it. We'd prefer
that you get identical results whether using VPATH or not, so this
is a bad thing.
Some of these places also printed their input file name(s), causing
an additional hazard of the same type.
Hence, establish a policy that thou shalt not print $0, nor input file
pathnames, into output files (they're still allowed in error messages,
though). Instead just write the script name verbatim. While we are at
it, we can make these annotations more useful by giving the script's
full relative path name within the PG source tree, eg instead of
Gen_fmgrtab.pl let's print src/backend/utils/Gen_fmgrtab.pl.
Not all of the changes made here actually affect any files shipped
in finished tarballs today, but it seems best to apply the policy
everyplace so that nobody copies unsafe code into places where it
could matter.
Christoph Berg and Tom Lane
Discussion: https://postgr.es/m/20171215102223.GB31812@msg.df7cb.de
2017-12-21 16:56:57 +01:00
|
|
|
* It has been GENERATED by src/backend/utils/Gen_fmgrtab.pl
|
2008-06-23 19:54:30 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2017-10-04 09:22:38 +02:00
|
|
|
#include "access/transam.h"
|
2008-06-23 19:54:30 +02:00
|
|
|
#include "utils/fmgrtab.h"
|
2016-12-28 18:00:00 +01:00
|
|
|
#include "utils/fmgrprotos.h"
|
2008-06-23 19:54:30 +02:00
|
|
|
|
|
|
|
|;
|
|
|
|
|
|
|
|
# Emit #define's and extern's -- only one per prosrc value
|
|
|
|
my %seenit;
|
2012-07-05 03:47:49 +02:00
|
|
|
foreach my $s (sort { $a->{oid} <=> $b->{oid} } @fmgr)
|
2008-06-23 19:54:30 +02:00
|
|
|
{
|
2012-07-05 03:47:49 +02:00
|
|
|
next if $seenit{ $s->{prosrc} };
|
|
|
|
$seenit{ $s->{prosrc} } = 1;
|
2017-03-27 04:24:13 +02:00
|
|
|
print $ofh "#define F_" . uc $s->{prosrc} . " $s->{oid}\n";
|
|
|
|
print $pfh "extern Datum $s->{prosrc}(PG_FUNCTION_ARGS);\n";
|
2008-06-23 19:54:30 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 09:22:38 +02:00
|
|
|
# Create the fmgr_builtins table, collect data for fmgr_builtin_oid_index
|
2017-03-27 04:24:13 +02:00
|
|
|
print $tfh "\nconst FmgrBuiltin fmgr_builtins[] = {\n";
|
2008-06-23 19:54:30 +02:00
|
|
|
my %bmap;
|
|
|
|
$bmap{'t'} = 'true';
|
|
|
|
$bmap{'f'} = 'false';
|
2017-10-04 09:22:38 +02:00
|
|
|
my @fmgr_builtin_oid_index;
|
|
|
|
my $fmgr_count = 0;
|
2012-07-05 03:47:49 +02:00
|
|
|
foreach my $s (sort { $a->{oid} <=> $b->{oid} } @fmgr)
|
2008-06-23 19:54:30 +02:00
|
|
|
{
|
2017-03-27 04:24:13 +02:00
|
|
|
print $tfh
|
2017-10-04 09:22:38 +02:00
|
|
|
" { $s->{oid}, \"$s->{prosrc}\", $s->{nargs}, $bmap{$s->{strict}}, $bmap{$s->{retset}}, $s->{prosrc} }";
|
|
|
|
|
|
|
|
$fmgr_builtin_oid_index[$s->{oid}] = $fmgr_count++;
|
|
|
|
|
|
|
|
if ($fmgr_count <= $#fmgr)
|
|
|
|
{
|
|
|
|
print $tfh ",\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print $tfh "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print $tfh "};\n";
|
|
|
|
|
|
|
|
print $tfh qq|
|
|
|
|
const int fmgr_nbuiltins = (sizeof(fmgr_builtins) / sizeof(FmgrBuiltin));
|
|
|
|
|;
|
|
|
|
|
|
|
|
|
|
|
|
# Create fmgr_builtins_oid_index table.
|
|
|
|
#
|
|
|
|
# Note that the array has to be filled up to FirstBootstrapObjectId,
|
|
|
|
# as we can't rely on zero initialization as 0 is a valid mapping.
|
|
|
|
print $tfh qq|
|
|
|
|
const uint16 fmgr_builtin_oid_index[FirstBootstrapObjectId] = {
|
|
|
|
|;
|
|
|
|
|
|
|
|
for (my $i = 0; $i < $FirstBootstrapObjectId; $i++)
|
|
|
|
{
|
|
|
|
my $oid = $fmgr_builtin_oid_index[$i];
|
|
|
|
|
|
|
|
# fmgr_builtin_oid_index is sparse, map nonexistant functions to
|
|
|
|
# InvalidOidBuiltinMapping
|
|
|
|
if (not defined $oid)
|
|
|
|
{
|
|
|
|
$oid = 'InvalidOidBuiltinMapping';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i + 1 == $FirstBootstrapObjectId)
|
|
|
|
{
|
|
|
|
print $tfh " $oid\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print $tfh " $oid,\n";
|
|
|
|
}
|
2008-06-23 19:54:30 +02:00
|
|
|
}
|
2017-10-04 09:22:38 +02:00
|
|
|
print $tfh "};\n";
|
|
|
|
|
2008-06-23 19:54:30 +02:00
|
|
|
|
|
|
|
# And add the file footers.
|
2017-03-27 04:24:13 +02:00
|
|
|
print $ofh "\n#endif /* FMGROIDS_H */\n";
|
|
|
|
print $pfh "\n#endif /* FMGRPROTOS_H */\n";
|
2008-06-23 19:54:30 +02:00
|
|
|
|
2017-03-27 04:24:13 +02:00
|
|
|
close($ofh);
|
|
|
|
close($pfh);
|
|
|
|
close($tfh);
|
2008-06-23 19:54:30 +02:00
|
|
|
|
|
|
|
# Finally, rename the completed files into place.
|
2017-05-18 01:01:23 +02:00
|
|
|
Catalog::RenameTempFile($oidsfile, $tmpext);
|
2016-12-28 18:00:00 +01:00
|
|
|
Catalog::RenameTempFile($protosfile, $tmpext);
|
2017-05-18 01:01:23 +02:00
|
|
|
Catalog::RenameTempFile($tabfile, $tmpext);
|
2010-01-05 02:06:57 +01:00
|
|
|
|
|
|
|
sub usage
|
|
|
|
{
|
2012-07-05 03:47:49 +02:00
|
|
|
die <<EOM;
|
2010-01-05 02:06:57 +01:00
|
|
|
Usage: perl -I [directory of Catalog.pm] Gen_fmgrtab.pl [path to pg_proc.h]
|
|
|
|
|
2017-12-21 23:07:32 +01:00
|
|
|
Gen_fmgrtab.pl generates fmgroids.h, fmgrprotos.h, and fmgrtab.c from
|
|
|
|
pg_proc.h
|
2010-01-05 02:06:57 +01:00
|
|
|
|
|
|
|
Report bugs to <pgsql-bugs\@postgresql.org>.
|
|
|
|
EOM
|
|
|
|
}
|
2008-06-23 19:54:30 +02:00
|
|
|
|
|
|
|
exit 0;
|