unused_oids 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/perl
  2. #----------------------------------------------------------------------
  3. #
  4. # unused_oids
  5. # Finds blocks of manually-assignable OIDs that have not already been
  6. # claimed by previous hackers. The main use is for finding available
  7. # OIDs for new internal functions. The numbers printed are inclusive
  8. # ranges of unused OIDs.
  9. #
  10. # Before using a large empty block, make sure you aren't about
  11. # to take over what was intended as expansion space for something
  12. # else.
  13. #
  14. # Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  15. # Portions Copyright (c) 1994, Regents of the University of California
  16. #
  17. # src/include/catalog/unused_oids
  18. #
  19. #----------------------------------------------------------------------
  20. use strict;
  21. use warnings;
  22. # Must run in src/include/catalog
  23. use FindBin;
  24. chdir $FindBin::RealBin or die "could not cd to $FindBin::RealBin: $!\n";
  25. use lib "$FindBin::RealBin/../../backend/catalog/";
  26. use Catalog;
  27. my @input_files = glob("pg_*.h");
  28. my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
  29. # Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
  30. my $FirstGenbkiObjectId =
  31. Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
  32. push @{$oids}, $FirstGenbkiObjectId;
  33. my $prev_oid = 0;
  34. my @sortedoids = sort { $a <=> $b } @{$oids};
  35. foreach my $oid (@sortedoids)
  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;
  46. }
  47. }
  48. $prev_oid = $oid;
  49. }
  50. my $suggestion;
  51. do
  52. {
  53. $suggestion = int(8000 + rand(2000));
  54. } while (grep(/^$suggestion$/, @{$oids}));
  55. my $navailable = 0;
  56. foreach my $oid (@sortedoids)
  57. {
  58. if ($oid > $suggestion)
  59. {
  60. $navailable = $oid - $suggestion;
  61. last;
  62. }
  63. }
  64. printf "Patches should use a more-or-less consecutive range of OIDs.\n";
  65. printf
  66. "Best practice is to start with a random choice in the range 8000-9999.\n";
  67. printf
  68. "Suggested random unused OID: $suggestion ($navailable consecutive OID(s) available starting here)\n";