Skip to content

Commit 5602265

Browse files
committed
Convert unused_oids and duplicate_oids to use Catalog.pm infrastructure.
unused_oids was previously a shell script, which of course didn't work at all on Windows. Also, commit 372728b introduced some other portability problems, as complained of by Stas Kelvich. We can improve matters by converting it to Perl. While we're at it, let's future-proof both this script and duplicate_oids to use Catalog.pm rather than having a bunch of ad-hoc logic for parsing catalog headers and .dat files. These scripts are thereby a bit slower, which doesn't seem like a problem for typical manual use. It is a little annoying for buildfarm purposes, but we should be able to fix that case by having genbki.pl make the check instead of parsing the headers twice. (That's not done in this commit, though.) Stas Kelvich, adjusted a bit by me Discussion: https://postgr.es/m/37D774E4-FE1F-437E-B3D2-593F314B7505@postgrespro.ru
1 parent 1eb3a09 commit 5602265

File tree

3 files changed

+89
-48
lines changed

3 files changed

+89
-48
lines changed

src/backend/catalog/Catalog.pm

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,55 @@ sub FindDefinedSymbolFromData
384384
die "no definition found for $symbol\n";
385385
}
386386

387+
# Extract an array of all the OIDs assigned in the specified catalog headers
388+
# and their associated data files (if any).
389+
sub FindAllOidsFromHeaders
390+
{
391+
my @input_files = @_;
392+
393+
my @oids = ();
394+
395+
foreach my $header (@input_files)
396+
{
397+
$header =~ /(.+)\.h$/
398+
or die "Input files need to be header files.\n";
399+
my $datfile = "$1.dat";
400+
401+
my $catalog = Catalog::ParseHeader($header);
402+
403+
# We ignore the pg_class OID and rowtype OID of bootstrap catalogs,
404+
# as those are expected to appear in the initial data for pg_class
405+
# and pg_type. For regular catalogs, include these OIDs.
406+
if (!$catalog->{bootstrap})
407+
{
408+
push @oids, $catalog->{relation_oid}
409+
if ($catalog->{relation_oid});
410+
push @oids, $catalog->{rowtype_oid} if ($catalog->{rowtype_oid});
411+
}
412+
413+
# Not all catalogs have a data file.
414+
if (-e $datfile)
415+
{
416+
my $catdata =
417+
Catalog::ParseData($datfile, $catalog->{columns}, 0);
418+
419+
foreach my $row (@$catdata)
420+
{
421+
push @oids, $row->{oid} if defined $row->{oid};
422+
}
423+
}
424+
425+
foreach my $toast (@{ $catalog->{toasting} })
426+
{
427+
push @oids, $toast->{toast_oid}, $toast->{toast_index_oid};
428+
}
429+
foreach my $index (@{ $catalog->{indexing} })
430+
{
431+
push @oids, $index->{index_oid};
432+
}
433+
}
434+
435+
return \@oids;
436+
}
437+
387438
1;

src/include/catalog/duplicate_oids

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
#!/usr/bin/perl
22

3+
use lib '../../backend/catalog/';
4+
use Catalog;
5+
36
use strict;
47
use warnings;
58

6-
BEGIN
7-
{
8-
@ARGV = (glob("pg_*.h"), glob("pg_*.dat"), qw(indexing.h toasting.h));
9-
}
9+
my @input_files = (glob("pg_*.h"), qw(indexing.h toasting.h));
10+
11+
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
1012

1113
my %oidcounts;
1214

13-
while (<>)
15+
foreach my $oid (@{$oids})
1416
{
15-
next if /^CATALOG\(.*BKI_BOOTSTRAP/;
16-
next
17-
unless /\boid *=> *'(\d+)'/
18-
|| /^CATALOG\([^,]*, *(\d+).*BKI_ROWTYPE_OID\((\d+),/
19-
|| /^CATALOG\([^,]*, *(\d+)/
20-
|| /^DECLARE_INDEX\([^,]*, *(\d+)/
21-
|| /^DECLARE_UNIQUE_INDEX\([^,]*, *(\d+)/
22-
|| /^DECLARE_TOAST\([^,]*, *(\d+), *(\d+)/;
23-
$oidcounts{$1}++;
24-
$oidcounts{$2}++ if $2;
17+
$oidcounts{$oid}++;
2518
}
2619

2720
my $found = 0;

src/include/catalog/unused_oids

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/perl
22
#
33
# unused_oids
44
#
@@ -15,43 +15,40 @@
1515
#
1616
# run this script in src/include/catalog.
1717
#
18+
use lib '../../backend/catalog/';
19+
use Catalog;
1820

21+
use strict;
22+
use warnings;
1923

20-
AWK="awk"
24+
my @input_files = (glob("pg_*.h"), qw(indexing.h toasting.h));
2125

22-
# Get FirstBootstrapObjectId from access/transam.h
23-
FIRSTOBJECTID=`grep '#define[ ]*FirstBootstrapObjectId' ../access/transam.h | $AWK '{ print $3 }'`
24-
export FIRSTOBJECTID
26+
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
2527

26-
# this part (down to the uniq step) should match the duplicate_oids script
27-
# note: we exclude BKI_BOOTSTRAP relations since they are expected to have
28-
# matching data entries in pg_class.dat and pg_type.dat
28+
# Also push FirstBootstrapObjectId to serve as a terminator for the last gap.
29+
my $FirstBootstrapObjectId =
30+
Catalog::FindDefinedSymbol('access/transam.h', [".."],
31+
'FirstBootstrapObjectId');
32+
push @{$oids}, $FirstBootstrapObjectId;
2933

30-
cat pg_*.h pg_*.dat toasting.h indexing.h |
31-
egrep -v -e '^CATALOG\(.*BKI_BOOTSTRAP' | \
32-
sed -n -e 's/.*\boid *=> *'\''\([0-9][0-9]*\)'\''.*$/\1/p' \
33-
-e 's/^CATALOG([^,]*, *\([0-9][0-9]*\).*BKI_ROWTYPE_OID(\([0-9][0-9]*\),.*$/\1,\2/p' \
34-
-e 's/^CATALOG([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
35-
-e 's/^DECLARE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
36-
-e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
37-
-e 's/^DECLARE_TOAST([^,]*, *\([0-9][0-9]*\), *\([0-9][0-9]*\).*$/\1,\2/p' | \
38-
tr ',' '\n' | \
39-
sort -n | \
40-
uniq | \
41-
$AWK '
42-
BEGIN {
43-
last = 0;
44-
}
45-
/^[0-9]/ {
46-
if ($1 > last + 1) {
47-
if ($1 > last + 2) {
48-
print last + 1, "-", $1 - 1;
49-
} else {
50-
print last + 1;
34+
my $prev_oid = 0;
35+
foreach my $oid (sort { $a <=> $b } @{$oids})
36+
{
37+
if ($oid > $prev_oid + 1)
38+
{
39+
if ($oid > $prev_oid + 2)
40+
{
41+
printf "%d - %d\n", $prev_oid + 1, $oid - 1;
42+
}
43+
else
44+
{
45+
printf "%d\n", $prev_oid + 1;
5146
}
5247
}
53-
last = $1;
48+
elsif ($oid == $prev_oid)
49+
{
50+
print "Duplicate oid detected: $oid\n";
51+
exit 1;
52+
}
53+
$prev_oid = $oid;
5454
}
55-
END {
56-
print last + 1, "-", ENVIRON["FIRSTOBJECTID"]-1;
57-
}'

0 commit comments

Comments
 (0)