Simplify initdb-time assignment of OIDs as I proposed yesterday, and
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 13 Apr 2005 18:54:57 +0000 (18:54 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 13 Apr 2005 18:54:57 +0000 (18:54 +0000)
avoid encroaching on the 'user' range of OIDs by allowing automatic
OID assignment to use values below 16k until we reach normal operation.

initdb not forced since this doesn't make any incompatible change;
however a lot of stuff will have different OIDs after your next initdb.

doc/src/sgml/bki.sgml
src/backend/access/transam/varsup.c
src/backend/access/transam/xlog.c
src/backend/catalog/genbki.sh
src/backend/storage/lmgr/lock.c
src/backend/utils/misc/guc.c
src/bin/pg_resetxlog/pg_resetxlog.c
src/include/access/transam.h
src/include/catalog/duplicate_oids
src/include/catalog/unused_oids

index cf2fa7343ee3ce38103787f54ba23b049bf5ec16..698147a6e046489eab380d0c5a26040da03f63d0 100644 (file)
@@ -167,8 +167,8 @@ $PostgreSQL$
       values and <replaceable
       class="parameter">oid_value</replaceable> for its OID.  If
       <replaceable class="parameter">oid_value</replaceable> is zero
-      (0) or the clause is omitted, then the next available OID is
-      used.
+      (0) or the clause is omitted, and the table has OIDs, then the
+      next available OID is assigned.
      </para>
 
      <para>
index 9c06febc58cd32c0e4284a8c8932b917d834a395..d4a1976506ce176db2b8f7d6fd8b967c59bd1865 100644 (file)
@@ -265,14 +265,36 @@ GetNewObjectId(void)
        /*
         * Check for wraparound of the OID counter.  We *must* not return 0
         * (InvalidOid); and as long as we have to check that, it seems a good
-        * idea to skip over everything below BootstrapObjectIdData too. (This
+        * idea to skip over everything below FirstNormalObjectId too. (This
         * basically just reduces the odds of OID collision right after a wrap
         * occurs.)  Note we are relying on unsigned comparison here.
+        *
+        * During initdb, we start the OID generator at FirstBootstrapObjectId,
+        * so we only enforce wrapping to that point when in bootstrap or
+        * standalone mode.  The first time through this routine after normal
+        * postmaster start, the counter will be forced up to FirstNormalObjectId.
+        * This mechanism leaves the OIDs between FirstBootstrapObjectId and
+        * FirstNormalObjectId available for automatic assignment during initdb,
+        * while ensuring they will never conflict with user-assigned OIDs.
         */
-       if (ShmemVariableCache->nextOid < ((Oid) BootstrapObjectIdData))
+       if (ShmemVariableCache->nextOid < ((Oid) FirstNormalObjectId))
        {
-               ShmemVariableCache->nextOid = BootstrapObjectIdData;
-               ShmemVariableCache->oidCount = 0;
+               if (IsPostmasterEnvironment)
+               {
+                       /* wraparound in normal environment */
+                       ShmemVariableCache->nextOid = FirstNormalObjectId;
+                       ShmemVariableCache->oidCount = 0;
+               }
+               else
+               {
+                       /* we may be bootstrapping, so don't enforce the full range */
+                       if (ShmemVariableCache->nextOid < ((Oid) FirstBootstrapObjectId))
+                       {
+                               /* wraparound in standalone environment? */
+                               ShmemVariableCache->nextOid = FirstBootstrapObjectId;
+                               ShmemVariableCache->oidCount = 0;
+                       }
+               }
        }
 
        /* If we run out of logged for use oids then we must log more */
index cafa931adf638e4c797ecb417577d8f5c5387d43..cf12c0c5e8d26e2f5ca0c7115aa32f93e0d84878 100644 (file)
@@ -3505,7 +3505,7 @@ BootStrapXLOG(void)
        checkPoint.undo = checkPoint.redo;
        checkPoint.ThisTimeLineID = ThisTimeLineID;
        checkPoint.nextXid = FirstNormalTransactionId;
-       checkPoint.nextOid = BootstrapObjectIdData;
+       checkPoint.nextOid = FirstBootstrapObjectId;
        checkPoint.time = time(NULL);
 
        ShmemVariableCache->nextXid = checkPoint.nextXid;
index fb109de99a969b46202e7acc9583cf4e3a778030..444350d9247e432ffcfb11291d56e12243000262 100644 (file)
@@ -121,15 +121,6 @@ for dir in $INCLUDE_DIRS; do
     fi
 done
 
-# Get FirstGenBKIObjectId from access/transam.h
-for dir in $INCLUDE_DIRS; do
-    if [ -f "$dir/access/transam.h" ]; then
-        BKIOBJECTID=`grep '^#define[   ]*FirstGenBKIObjectId' $dir/access/transam.h | $AWK '{ print $3 }'`
-        break
-    fi
-done
-export BKIOBJECTID
-
 touch ${OUTPUT_PREFIX}.description.$$
 
 # ----------------
@@ -173,8 +164,7 @@ sed -e "s/;[        ]*$//g" \
 #         contents of a catalog definition.
 #      reln_open is a flag indicating when we are processing DATA lines.
 #         (i.e. have a relation open and need to close it)
-#      nextbkioid is the next OID available for automatic assignment.
-#      oid is the most recently seen or assigned oid.
+#      oid is the most recently seen oid, or 0 if none in the last DATA line.
 # ----------------
 BEGIN {
        inside = 0;
@@ -184,7 +174,6 @@ BEGIN {
        nc = 0;
        reln_open = 0;
        comment_level = 0;
-       nextbkioid = ENVIRON["BKIOBJECTID"];
        oid = 0;
 }
 
@@ -202,9 +191,8 @@ comment_level > 0 { next; }
 
 # ----------------
 #      DATA() statements are basically passed right through after
-#      stripping off the DATA( and the ) on the end.  However,
-#      if we see "OID = 0" then we should assign an oid from nextbkioid.
-#      Remember any explicit or assigned OID for use by DESCR().
+#      stripping off the DATA( and the ) on the end.
+#      Remember any explicit OID for use by DESCR().
 # ----------------
 /^DATA\(/ {
        data = substr($0, 6, length($0) - 6);
@@ -213,12 +201,6 @@ comment_level > 0 { next; }
        if (nf >= 4 && datafields[1] == "insert" && datafields[2] == "OID" && datafields[3] == "=")
        {
                oid = datafields[4];
-               if (oid == 0)
-               {
-                       oid = nextbkioid;
-                       nextbkioid++;
-                       sub("OID *= *0", "OID = " oid, data);
-               }
        }
        print data;
        next;
index e9f6050350289a1faff9373db50195f3e2da684b..9c61b88feee92dd5a19aafbeb6aee3e3d4faf13c 100644 (file)
@@ -97,7 +97,7 @@ static const char *const lock_mode_names[] =
  * --------
  */
 
-int                    Trace_lock_oidmin = BootstrapObjectIdData;
+int                    Trace_lock_oidmin = FirstNormalObjectId;
 bool           Trace_locks = false;
 bool           Trace_userlocks = false;
 int                    Trace_lock_table = 0;
index ada686abefe3a82923b22321cdf7da14900ca5d7..a417d71468c524e5eb47baccfe4f512dc2aeb861 100644 (file)
@@ -1090,7 +1090,7 @@ static struct config_int ConfigureNamesInt[] =
                        GUC_NOT_IN_SAMPLE
                },
                &Trace_lock_oidmin,
-               BootstrapObjectIdData, 0, INT_MAX, NULL, NULL
+               FirstNormalObjectId, 0, INT_MAX, NULL, NULL
        },
        {
                {"trace_lock_table", PGC_SUSET, DEVELOPER_OPTIONS,
index fc0e805fb38a6c9cf2c665a597b1e7a1385c6207..51242f11d25e59ff2d55b7109677167dbb5e489e 100644 (file)
@@ -404,7 +404,7 @@ GuessControlValues(void)
        ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
        ControlFile.checkPointCopy.ThisTimeLineID = 1;
        ControlFile.checkPointCopy.nextXid = (TransactionId) 514;       /* XXX */
-       ControlFile.checkPointCopy.nextOid = BootstrapObjectIdData;
+       ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
        ControlFile.checkPointCopy.time = time(NULL);
 
        ControlFile.state = DB_SHUTDOWNED;
index 1c41107164190d8e6bd0dac00230fdabf816bda3..41a7373c58df624797c6eddcc4052b0c75840041 100644 (file)
  *             OIDs 1-9999 are reserved for manual assignment (see the files
  *             in src/include/catalog/).
  *
- *             OIDS 10000-16383 are reserved for assignment by genbki.sh.
+ *             OIDS 10000-16383 are reserved for assignment during initdb
+ *             using the OID generator.  (We start the generator at 10000.)
  *
- *             OIDs beginning at 16384 are assigned at runtime from the OID
- *             generator.      (The first few of these will be assigned during initdb,
- *             to objects created after the initial BKI script processing.)
+ *             OIDs beginning at 16384 are assigned from the OID generator
+ *             during normal multiuser operation.  (We force the generator up to
+ *             16384 as soon as we are in normal operation.)
  *
  * The choices of 10000 and 16384 are completely arbitrary, and can be moved
  * if we run low on OIDs in either category.  Changing the macros below
  * should be sufficient to do this.
  *
- * NOTE: if the OID generator wraps around, we should skip over OIDs 0-16383
+ * NOTE: if the OID generator wraps around, we skip over OIDs 0-16383
  * and resume with 16384.  This minimizes the odds of OID conflict, by not
  * reassigning OIDs that might have been assigned during initdb.
  * ----------
  */
-#define FirstGenBKIObjectId   10000
-#define BootstrapObjectIdData 16384
+#define FirstBootstrapObjectId 10000
+#define FirstNormalObjectId            16384
 
 /*
  * VariableCache is placed in shmem and used by
index ebcea8f2eca821ca3f4395ddb622cbf670eaf532..3e8fd41ccc8f7c17aa187b9997b68f9e10ab1b99 100755 (executable)
@@ -2,45 +2,18 @@
 #
 # duplicate_oids
 #
-# finds oids that are duplicated in the system tables.
+# $PostgreSQL$
 #
-
-FILES=`ls pg_*.h`
-
+# finds manually-assigned oids that are duplicated in the system tables.
 #
-# The previous version did not use the -d option on uniq
-# so check here that it is supported.
-# Otherwise, use the old algorithm
+# run this script in src/include/catalog.
 #
 
-if [ `uniq -d < /dev/null > /dev/null 2>&1` ]; then
-  echo "uniq -d is not supported on your platform."
-  echo "Please report this to pgsql-hackers@postgresql.org"
-
-egrep '^DATA' $FILES | \
-       sed -e 's/^.*OID[^=]*=[^0-9]*//' -e 's/[^0-9].*$//' | \
-       sort -n >/tmp/alloids.$$
-uniq /tmp/alloids.$$ >/tmp/uniqoids.$$
-
-diff -u /tmp/alloids.$$ /tmp/uniqoids.$$ | \
-       grep -v '/tmp/' | \
-       grep '^-' | \
-       sed -e 's/^-//' | \
-       grep -v '^0$' | \
-       uniq
-rm /tmp/alloids.$$
-rm /tmp/uniqoids.$$
-
-else
-
-#  echo "uniq -d is supported on this platform."
-#  echo "Will omit the use of temporary files."
+FILES=`ls pg_*.h`
 
 egrep '^DATA' $FILES | \
        sed -e 's/^.*OID[^=]*=[^0-9]*//' -e 's/[^0-9].*$//' | \
        sort -n | uniq -d | \
-       egrep -v '^[0]*$'
-
-fi
+       egrep -v '^0*$'
 
-exit
+exit 0
index 0799ee90dc2cabe01f12f7f6766d2b0d3fe9120f..9a557529a55f98dd221bc059f552b0d948bda7fc 100755 (executable)
@@ -1,20 +1,17 @@
 #!/bin/sh
+#
 # unused_oids
 #
 # $PostgreSQL$
 #
-#      finds blocks of oids that have not already been claimed by 
-#      post_hackers for internal purposes.  primarily useful for
-#      finding valid oids for new internal function oids.  the numbers
-#      printed are inclusive ranges of valid (unused) oids.
+#      finds blocks of manually-assignable oids that have not already been
+#      claimed by post_hackers.  primarily useful 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.  also, before using a number, do a "grepsrc" to make sure 
-#      that someone isn't using a literal numeric constant somewhere..
-#
-#      non-berkeley post_hackers should probably not try to use oids 
-#      less than the highest one that comes with the distributed source.
+#      else.
 #
 #      run this script in src/include/catalog.
 #
@@ -22,9 +19,9 @@
 
 AWK="awk"
 
-# Get FirstGenBKIObjectId from access/transam.h
-BKIOBJECTID=`grep '#define[    ]*FirstGenBKIObjectId' ../access/transam.h | $AWK '{ print $3 }'`
-export BKIOBJECTID
+# Get FirstBootstrapObjectId from access/transam.h
+FIRSTOBJECTID=`grep '#define[  ]*FirstBootstrapObjectId' ../access/transam.h | $AWK '{ print $3 }'`
+export FIRSTOBJECTID
 
 egrep '^DATA' pg_*.h | \
        sed -e 's/^.*OID[^=]*=[^0-9]*//' -e 's/[^0-9].*$//' | \
@@ -45,5 +42,5 @@ BEGIN {
        last = $1;
 }
 END {
-       print last + 1, "-", ENVIRON["BKIOBJECTID"]-1;
+       print last + 1, "-", ENVIRON["FIRSTOBJECTID"]-1;
 }'