• Peter Geoghegan's avatar
    Show specific OID suggestion in unused_oids output. · 98eab30b
    Peter Geoghegan authored
    Commit a6417078 established a new project policy around OID assignment:
    new patches are encouraged to choose a random OID in the 8000..9999
    range when a manually-assigned OID is required (if multiple OIDs are
    required, a consecutive block of OIDs starting from the random point
    should be used).  Catalog entries added by committed patches that use
    OIDs from this "unstable" range are renumbered after feature freeze.
    This practice minimizes OID collisions among concurrently-developed
    patches.
    
    Show a specific random OID suggestion when the unused_oids script is
    run.  This makes it easy for patch authors to use a random OID from the
    unstable range, per the new policy.
    
    Author: Julien Rouhaud, Peter Geoghegan
    Reviewed-By: Tom Lane
    Discussion: https://postgr.es/m/CAH2-WzkkRs2ScmuBQ7xWi7xzp7fC1B3w0Nt8X+n4rBw5k+Z=zA@mail.gmail.com
    98eab30b
unused_oids 2.05 KB
#!/usr/bin/perl
#----------------------------------------------------------------------
#
# unused_oids
#    Finds blocks of manually-assignable OIDs that have not already been
#    claimed by previous hackers.  The main use is for finding available
#    OIDs for new internal functions.  The numbers printed are inclusive
#    ranges of unused OIDs.
#
#    Before using a large empty block, make sure you aren't about
#    to take over what was intended as expansion space for something
#    else.
#
# Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# src/include/catalog/unused_oids
#
#----------------------------------------------------------------------

use strict;
use warnings;

# Must run in src/include/catalog
use FindBin;
chdir $FindBin::RealBin or die "could not cd to $FindBin::RealBin: $!\n";

use lib "$FindBin::RealBin/../../backend/catalog/";
use Catalog;

my @input_files = (glob("pg_*.h"), qw(indexing.h toasting.h));

my $oids = Catalog::FindAllOidsFromHeaders(@input_files);

# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
  Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
push @{$oids}, $FirstGenbkiObjectId;

my $prev_oid = 0;
my @sortedoids = sort { $a <=> $b } @{$oids};
foreach my $oid (@sortedoids)
{
	if ($oid > $prev_oid + 1)
	{
		if ($oid > $prev_oid + 2)
		{
			printf "%d - %d\n", $prev_oid + 1, $oid - 1;
		}
		else
		{
			printf "%d\n", $prev_oid + 1;
		}
	}
	$prev_oid = $oid;
}

my $suggestion;
do
{
	$suggestion = int(8000 + rand(2000));
} while (grep(/^$suggestion$/, @{$oids}));

my $navailable = 0;
foreach my $oid (@sortedoids)
{
	if ($oid > $suggestion)
	{
		$navailable = $oid - $suggestion;
		last;
	}
}

printf "Patches should use a more-or-less consecutive range of OIDs.\n";
printf
  "Best practice is to start with a random choice in the range 8000-9999.\n";
printf
  "Suggested random unused OID: $suggestion ($navailable consecutive OID(s) available starting here)\n";