<!--
-$PostgreSQL: pgsql/doc/src/sgml/bki.sgml,v 1.14 2005/03/29 19:44:22 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/bki.sgml,v 1.15 2005/04/13 18:54:55 tgl Exp $
  -->
 
 <chapter id="bki">
       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>
 
  * Copyright (c) 2000-2005, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/access/transam/varsup.c,v 1.62 2005/02/20 21:46:48 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/access/transam/varsup.c,v 1.63 2005/04/13 18:54:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
    /*
     * 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 */
 
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.183 2005/03/29 03:01:30 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.184 2005/04/13 18:54:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
    checkPoint.undo = checkPoint.redo;
    checkPoint.ThisTimeLineID = ThisTimeLineID;
    checkPoint.nextXid = FirstNormalTransactionId;
-   checkPoint.nextOid = BootstrapObjectIdData;
+   checkPoint.nextOid = FirstBootstrapObjectId;
    checkPoint.time = time(NULL);
 
    ShmemVariableCache->nextXid = checkPoint.nextXid;
 
 #
 #
 # IDENTIFICATION
-#    $PostgreSQL: pgsql/src/backend/catalog/genbki.sh,v 1.33 2005/03/29 00:16:55 tgl Exp $
+#    $PostgreSQL: pgsql/src/backend/catalog/genbki.sh,v 1.34 2005/04/13 18:54:56 tgl Exp $
 #
 # NOTES
 #    non-essential whitespace is removed from the generated file.
     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.$$
 
 # ----------------
 #     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;
    nc = 0;
    reln_open = 0;
    comment_level = 0;
-   nextbkioid = ENVIRON["BKIOBJECTID"];
    oid = 0;
 }
 
 
 # ----------------
 #  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);
    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;
 
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.148 2005/03/11 03:52:06 neilc Exp $
+ *   $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.149 2005/04/13 18:54:56 tgl Exp $
  *
  * NOTES
  *   Outside modules can create a lock table and acquire/release
  * --------
  */
 
-int            Trace_lock_oidmin = BootstrapObjectIdData;
+int            Trace_lock_oidmin = FirstNormalObjectId;
 bool       Trace_locks = false;
 bool       Trace_userlocks = false;
 int            Trace_lock_table = 0;
 
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.258 2005/04/08 00:59:59 neilc Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.259 2005/04/13 18:54:56 tgl Exp $
  *
  *--------------------------------------------------------------------
  */
            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,
 
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.30 2005/03/29 03:01:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.31 2005/04/13 18:54:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
    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;
 
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/transam.h,v 1.53 2005/02/20 21:46:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/transam.h,v 1.54 2005/04/13 18:54:57 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
  *     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
 
 #
 # duplicate_oids
 #
-# finds oids that are duplicated in the system tables.
+# $PostgreSQL: pgsql/src/include/catalog/duplicate_oids,v 1.6 2005/04/13 18:54:57 tgl Exp $
 #
-
-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."
-
-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
 
 #!/bin/sh
+#
 # unused_oids
 #
-# $PostgreSQL: pgsql/src/include/catalog/unused_oids,v 1.5 2003/11/29 19:52:08 pgsql Exp $
+# $PostgreSQL: pgsql/src/include/catalog/unused_oids,v 1.6 2005/04/13 18:54:57 tgl Exp $
 #
-#  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.
 #
 
 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].*$//' | \
    last = $1;
 }
 END {
-   print last + 1, "-", ENVIRON["BKIOBJECTID"]-1;
+   print last + 1, "-", ENVIRON["FIRSTOBJECTID"]-1;
 }'