]> granicus.if.org Git - postgresql/blobdiff - src/backend/access/transam/varsup.c
Fix initialization of fake LSN for unlogged relations
[postgresql] / src / backend / access / transam / varsup.c
index 75a568f8fde634bea626df1c45f750bccb4c482e..b18eee42d4855899e5a13666f70f5861496b1baa 100644 (file)
 /*-------------------------------------------------------------------------
  *
  * varsup.c
- *       postgres variable relation support routines
- *
- * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
- * Portions Copyright (c) 1994, Regents of the University of California
+ *       postgres OID & XID variables support routines
  *
+ * Copyright (c) 2000-2019, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.33 2000/11/20 16:47:30 petere Exp $
+ *       src/backend/access/transam/varsup.c
  *
  *-------------------------------------------------------------------------
  */
-#include "postgres.h"
 
-#ifdef XLOG
-#include "xlog_varsup.c"
-#else
+#include "postgres.h"
 
-#include "access/heapam.h"
-#include "catalog/catname.h"
+#include "access/clog.h"
+#include "access/commit_ts.h"
+#include "access/subtrans.h"
+#include "access/transam.h"
+#include "access/xact.h"
+#include "access/xlog.h"
+#include "commands/dbcommands.h"
+#include "miscadmin.h"
+#include "postmaster/autovacuum.h"
+#include "storage/pmsignal.h"
 #include "storage/proc.h"
+#include "utils/syscache.h"
 
-static void GetNewObjectIdBlock(Oid *oid_return, int oid_block_size);
-static void VariableRelationGetNextOid(Oid *oid_return);
-static void VariableRelationGetNextXid(TransactionId *xidP);
-static void VariableRelationPutNextOid(Oid oid);
 
-/* ---------------------
- *             spin lock for oid generation
- * ---------------------
- */
-int                    OidGenLockId;
+/* Number of OIDs to prefetch (preallocate) per XLOG write */
+#define VAR_OID_PREFETCH               8192
 
-/* ---------------------
- *             pointer to "variable cache" in shared memory (set up by shmem.c)
- * ---------------------
- */
+/* pointer to "variable cache" in shared memory (set up by shmem.c) */
 VariableCache ShmemVariableCache = NULL;
 
 
-/* ----------------------------------------------------------------
- *                       variable relation query/update routines
- * ----------------------------------------------------------------
- */
-
-/* --------------------------------
- *             VariableRelationGetNextXid
- * --------------------------------
+/*
+ * Allocate the next FullTransactionId for a new transaction or
+ * subtransaction.
+ *
+ * The new XID is also stored into MyPgXact before returning.
+ *
+ * Note: when this is called, we are actually already inside a valid
+ * transaction, since XIDs are now not allocated until the transaction
+ * does something.  So it is safe to do a database lookup if we want to
+ * issue a warning about XID wrap.
  */
-static void
-VariableRelationGetNextXid(TransactionId *xidP)
+FullTransactionId
+GetNewTransactionId(bool isSubXact)
 {
-       Buffer          buf;
-       VariableRelationContents var;
+       FullTransactionId full_xid;
+       TransactionId xid;
 
-       /* ----------------
-        * We assume that a spinlock has been acquired to guarantee
-        * exclusive access to the variable relation.
-        * ----------------
+       /*
+        * Workers synchronize transaction state at the beginning of each parallel
+        * operation, so we can't account for new XIDs after that point.
         */
+       if (IsInParallelMode())
+               elog(ERROR, "cannot assign TransactionIds during a parallel operation");
 
-       /* ----------------
-        *      do nothing before things are initialized
-        * ----------------
-        */
-       if (!RelationIsValid(VariableRelation))
-               return;
-
-       /* ----------------
-        *      read the variable page, get the the nextXid field and
-        *      release the buffer
-        * ----------------
+       /*
+        * During bootstrap initialization, we return the special bootstrap
+        * transaction id.
         */
-       buf = ReadBuffer(VariableRelation, 0);
-
-       if (!BufferIsValid(buf))
+       if (IsBootstrapProcessingMode())
        {
-               SpinRelease(OidGenLockId);
-               elog(ERROR, "VariableRelationGetNextXid: ReadBuffer failed");
+               Assert(!isSubXact);
+               MyPgXact->xid = BootstrapTransactionId;
+               return FullTransactionIdFromEpochAndXid(0, BootstrapTransactionId);
        }
 
-       var = (VariableRelationContents) BufferGetBlock(buf);
-
-       TransactionIdStore(var->nextXidData, xidP);
-
-       ReleaseBuffer(buf);
-}
-
-/* --------------------------------
- *             VariableRelationPutNextXid
- * --------------------------------
- */
-void
-VariableRelationPutNextXid(TransactionId xid)
-{
-       Buffer          buf;
-       VariableRelationContents var;
+       /* safety check, we should never get this far in a HS standby */
+       if (RecoveryInProgress())
+               elog(ERROR, "cannot assign TransactionIds during recovery");
 
-       /* ----------------
-        * We assume that a spinlock has been acquired to guarantee
-        * exclusive access to the variable relation.
-        * ----------------
-        */
+       LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
 
-       /* ----------------
-        *      do nothing before things are initialized
-        * ----------------
-        */
-       if (!RelationIsValid(VariableRelation))
-               return;
+       full_xid = ShmemVariableCache->nextFullXid;
+       xid = XidFromFullTransactionId(full_xid);
 
-       /* ----------------
-        *      read the variable page, update the nextXid field and
-        *      write the page back out to disk (with immediate write).
-        * ----------------
+       /*----------
+        * Check to see if it's safe to assign another XID.  This protects against
+        * catastrophic data loss due to XID wraparound.  The basic rules are:
+        *
+        * If we're past xidVacLimit, start trying to force autovacuum cycles.
+        * If we're past xidWarnLimit, start issuing warnings.
+        * If we're past xidStopLimit, refuse to execute transactions, unless
+        * we are running in single-user mode (which gives an escape hatch
+        * to the DBA who somehow got past the earlier defenses).
+        *
+        * Note that this coding also appears in GetNewMultiXactId.
+        *----------
         */
-       buf = ReadBuffer(VariableRelation, 0);
-
-       if (!BufferIsValid(buf))
+       if (TransactionIdFollowsOrEquals(xid, ShmemVariableCache->xidVacLimit))
        {
-               SpinRelease(OidGenLockId);
-               elog(ERROR, "VariableRelationPutNextXid: ReadBuffer failed");
-       }
-
-       var = (VariableRelationContents) BufferGetBlock(buf);
+               /*
+                * For safety's sake, we release XidGenLock while sending signals,
+                * warnings, etc.  This is not so much because we care about
+                * preserving concurrency in this situation, as to avoid any
+                * possibility of deadlock while doing get_database_name(). First,
+                * copy all the shared values we'll need in this path.
+                */
+               TransactionId xidWarnLimit = ShmemVariableCache->xidWarnLimit;
+               TransactionId xidStopLimit = ShmemVariableCache->xidStopLimit;
+               TransactionId xidWrapLimit = ShmemVariableCache->xidWrapLimit;
+               Oid                     oldest_datoid = ShmemVariableCache->oldestXidDB;
 
-       TransactionIdStore(xid, &(var->nextXidData));
+               LWLockRelease(XidGenLock);
 
-       FlushBuffer(buf, true, true);
-}
+               /*
+                * To avoid swamping the postmaster with signals, we issue the autovac
+                * request only once per 64K transaction starts.  This still gives
+                * plenty of chances before we get into real trouble.
+                */
+               if (IsUnderPostmaster && (xid % 65536) == 0)
+                       SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
+
+               if (IsUnderPostmaster &&
+                       TransactionIdFollowsOrEquals(xid, xidStopLimit))
+               {
+                       char       *oldest_datname = get_database_name(oldest_datoid);
+
+                       /* complain even if that DB has disappeared */
+                       if (oldest_datname)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                                errmsg("database is not accepting commands to avoid wraparound data loss in database \"%s\"",
+                                                               oldest_datname),
+                                                errhint("Stop the postmaster and vacuum that database in single-user mode.\n"
+                                                                "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
+                       else
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                                errmsg("database is not accepting commands to avoid wraparound data loss in database with OID %u",
+                                                               oldest_datoid),
+                                                errhint("Stop the postmaster and vacuum that database in single-user mode.\n"
+                                                                "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
+               }
+               else if (TransactionIdFollowsOrEquals(xid, xidWarnLimit))
+               {
+                       char       *oldest_datname = get_database_name(oldest_datoid);
+
+                       /* complain even if that DB has disappeared */
+                       if (oldest_datname)
+                               ereport(WARNING,
+                                               (errmsg("database \"%s\" must be vacuumed within %u transactions",
+                                                               oldest_datname,
+                                                               xidWrapLimit - xid),
+                                                errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
+                                                                "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
+                       else
+                               ereport(WARNING,
+                                               (errmsg("database with OID %u must be vacuumed within %u transactions",
+                                                               oldest_datoid,
+                                                               xidWrapLimit - xid),
+                                                errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
+                                                                "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
+               }
+
+               /* Re-acquire lock and start over */
+               LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
+               full_xid = ShmemVariableCache->nextFullXid;
+               xid = XidFromFullTransactionId(full_xid);
+       }
 
-/* --------------------------------
- *             VariableRelationGetNextOid
- * --------------------------------
- */
-static void
-VariableRelationGetNextOid(Oid *oid_return)
-{
-       Buffer          buf;
-       VariableRelationContents var;
+       /*
+        * If we are allocating the first XID of a new page of the commit log,
+        * zero out that commit-log page before returning. We must do this while
+        * holding XidGenLock, else another xact could acquire and commit a later
+        * XID before we zero the page.  Fortunately, a page of the commit log
+        * holds 32K or more transactions, so we don't have to do this very often.
+        *
+        * Extend pg_subtrans and pg_commit_ts too.
+        */
+       ExtendCLOG(xid);
+       ExtendCommitTs(xid);
+       ExtendSUBTRANS(xid);
 
-       /* ----------------
-        * We assume that a spinlock has been acquired to guarantee
-        * exclusive access to the variable relation.
-        * ----------------
+       /*
+        * Now advance the nextFullXid counter.  This must not happen until after
+        * we have successfully completed ExtendCLOG() --- if that routine fails,
+        * we want the next incoming transaction to try it again.  We cannot
+        * assign more XIDs until there is CLOG space for them.
         */
+       FullTransactionIdAdvance(&ShmemVariableCache->nextFullXid);
 
-       /* ----------------
-        *      if the variable relation is not initialized, then we
-        *      assume we are running at bootstrap time and so we return
-        *      an invalid object id (this path should never be taken, probably).
-        * ----------------
+       /*
+        * We must store the new XID into the shared ProcArray before releasing
+        * XidGenLock.  This ensures that every active XID older than
+        * latestCompletedXid is present in the ProcArray, which is essential for
+        * correct OldestXmin tracking; see src/backend/access/transam/README.
+        *
+        * Note that readers of PGXACT xid fields should be careful to fetch the
+        * value only once, rather than assume they can read a value multiple
+        * times and get the same answer each time.  Note we are assuming that
+        * TransactionId and int fetch/store are atomic.
+        *
+        * The same comments apply to the subxact xid count and overflow fields.
+        *
+        * Use of a write barrier prevents dangerous code rearrangement in this
+        * function; other backends could otherwise e.g. be examining my subxids
+        * info concurrently, and we don't want them to see an invalid
+        * intermediate state, such as an incremented nxids before the array entry
+        * is filled.
+        *
+        * Other processes that read nxids should do so before reading xids
+        * elements with a pg_read_barrier() in between, so that they can be sure
+        * not to read an uninitialized array element; see
+        * src/backend/storage/lmgr/README.barrier.
+        *
+        * If there's no room to fit a subtransaction XID into PGPROC, set the
+        * cache-overflowed flag instead.  This forces readers to look in
+        * pg_subtrans to map subtransaction XIDs up to top-level XIDs. There is a
+        * race-condition window, in that the new XID will not appear as running
+        * until its parent link has been placed into pg_subtrans. However, that
+        * will happen before anyone could possibly have a reason to inquire about
+        * the status of the XID, so it seems OK.  (Snapshots taken during this
+        * window *will* include the parent XID, so they will deliver the correct
+        * answer later on when someone does have a reason to inquire.)
         */
-       if (!RelationIsValid(VariableRelation))
+       if (!isSubXact)
+               MyPgXact->xid = xid;    /* LWLockRelease acts as barrier */
+       else
        {
-               (*oid_return) = InvalidOid;
-               return;
+               int                     nxids = MyPgXact->nxids;
+
+               if (nxids < PGPROC_MAX_CACHED_SUBXIDS)
+               {
+                       MyProc->subxids.xids[nxids] = xid;
+                       pg_write_barrier();
+                       MyPgXact->nxids = nxids + 1;
+               }
+               else
+                       MyPgXact->overflowed = true;
        }
 
-       /* ----------------
-        *      read the variable page, get the the nextOid field and
-        *      release the buffer
-        * ----------------
-        */
-       buf = ReadBuffer(VariableRelation, 0);
+       LWLockRelease(XidGenLock);
 
-       if (!BufferIsValid(buf))
-       {
-               SpinRelease(OidGenLockId);
-               elog(ERROR, "VariableRelationGetNextOid: ReadBuffer failed");
-       }
+       return full_xid;
+}
 
-       var = (VariableRelationContents) BufferGetBlock(buf);
+/*
+ * Read nextFullXid but don't allocate it.
+ */
+FullTransactionId
+ReadNextFullTransactionId(void)
+{
+       FullTransactionId fullXid;
 
-       (*oid_return) = var->nextOid;
+       LWLockAcquire(XidGenLock, LW_SHARED);
+       fullXid = ShmemVariableCache->nextFullXid;
+       LWLockRelease(XidGenLock);
 
-       ReleaseBuffer(buf);
+       return fullXid;
 }
 
-/* --------------------------------
- *             VariableRelationPutNextOid
- * --------------------------------
+/*
+ * Advance nextFullXid to the value after a given xid.  The epoch is inferred.
+ * This must only be called during recovery or from two-phase start-up code.
  */
-static void
-VariableRelationPutNextOid(Oid oid)
+void
+AdvanceNextFullTransactionIdPastXid(TransactionId xid)
 {
-       Buffer          buf;
-       VariableRelationContents var;
+       FullTransactionId newNextFullXid;
+       TransactionId next_xid;
+       uint32          epoch;
 
-       /* ----------------
-        * We assume that a spinlock has been acquired to guarantee
-        * exclusive access to the variable relation.
-        * ----------------
+       /*
+        * It is safe to read nextFullXid without a lock, because this is only
+        * called from the startup process or single-process mode, meaning that no
+        * other process can modify it.
         */
+       Assert(AmStartupProcess() || !IsUnderPostmaster);
 
-       /* ----------------
-        *      do nothing before things are initialized
-        * ----------------
-        */
-       if (!RelationIsValid(VariableRelation))
+       /* Fast return if this isn't an xid high enough to move the needle. */
+       next_xid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
+       if (!TransactionIdFollowsOrEquals(xid, next_xid))
                return;
 
-       /* ----------------
-        *      read the variable page, update the nextXid field and
-        *      write the page back out to disk.
-        * ----------------
+       /*
+        * Compute the FullTransactionId that comes after the given xid.  To do
+        * this, we preserve the existing epoch, but detect when we've wrapped
+        * into a new epoch.  This is necessary because WAL records and 2PC state
+        * currently contain 32 bit xids.  The wrap logic is safe in those cases
+        * because the span of active xids cannot exceed one epoch at any given
+        * point in the WAL stream.
         */
-       buf = ReadBuffer(VariableRelation, 0);
-
-       if (!BufferIsValid(buf))
-       {
-               SpinRelease(OidGenLockId);
-               elog(ERROR, "VariableRelationPutNextOid: ReadBuffer failed");
-       }
-
-       var = (VariableRelationContents) BufferGetBlock(buf);
-
-       var->nextOid = oid;
+       TransactionIdAdvance(xid);
+       epoch = EpochFromFullTransactionId(ShmemVariableCache->nextFullXid);
+       if (unlikely(xid < next_xid))
+               ++epoch;
+       newNextFullXid = FullTransactionIdFromEpochAndXid(epoch, xid);
 
-       WriteBuffer(buf);
+       /*
+        * We still need to take a lock to modify the value when there are
+        * concurrent readers.
+        */
+       LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
+       ShmemVariableCache->nextFullXid = newNextFullXid;
+       LWLockRelease(XidGenLock);
 }
 
-/* ----------------------------------------------------------------
- *                             transaction id generation support
- * ----------------------------------------------------------------
- */
-
-/* ----------------
- *             GetNewTransactionId
- *
- *             Transaction IDs are allocated via a cache in shared memory.
- *             Each time we need more IDs, we advance the "next XID" value
- *             in pg_variable by VAR_XID_PREFETCH and set the cache to
- *             show that many XIDs as available.  Then, allocating those XIDs
- *             requires just a spinlock and not a buffer read/write cycle.
- *
- *             Since the cache is shared across all backends, cached but unused
- *             XIDs are not lost when a backend exits, only when the postmaster
- *             quits or forces shared memory reinit.  So we can afford to have
- *             a pretty big value of VAR_XID_PREFETCH.
+/*
+ * Advance the cluster-wide value for the oldest valid clog entry.
  *
- *             This code does not worry about initializing the transaction counter
- *             (see transam.c's InitializeTransactionLog() for that).  We also
- *             ignore the possibility that the counter could someday wrap around.
- * ----------------
+ * We must acquire CLogTruncationLock to advance the oldestClogXid. It's not
+ * necessary to hold the lock during the actual clog truncation, only when we
+ * advance the limit, as code looking up arbitrary xids is required to hold
+ * CLogTruncationLock from when it tests oldestClogXid through to when it
+ * completes the clog lookup.
  */
-
-#define VAR_XID_PREFETCH               1024
-
 void
-GetNewTransactionId(TransactionId *xid)
+AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
 {
-
-       /* ----------------
-        *      during bootstrap initialization, we return the special
-        *      bootstrap transaction id.
-        * ----------------
-        */
-       if (AMI_OVERRIDE)
+       LWLockAcquire(CLogTruncationLock, LW_EXCLUSIVE);
+       if (TransactionIdPrecedes(ShmemVariableCache->oldestClogXid,
+                                                         oldest_datfrozenxid))
        {
-               TransactionIdStore(AmiTransactionId, xid);
-               return;
-       }
-
-       SpinAcquire(OidGenLockId);      /* not good for concurrency... */
-
-       if (ShmemVariableCache->xid_count == 0)
-       {
-               TransactionId nextid;
-
-               VariableRelationGetNextXid(&nextid);
-               TransactionIdStore(nextid, &(ShmemVariableCache->nextXid));
-               ShmemVariableCache->xid_count = VAR_XID_PREFETCH;
-               TransactionIdAdd(&nextid, VAR_XID_PREFETCH);
-               VariableRelationPutNextXid(nextid);
+               ShmemVariableCache->oldestClogXid = oldest_datfrozenxid;
        }
-
-       TransactionIdStore(ShmemVariableCache->nextXid, xid);
-       TransactionIdAdd(&(ShmemVariableCache->nextXid), 1);
-       (ShmemVariableCache->xid_count)--;
-
-       if (MyProc != (PROC *) NULL)
-               MyProc->xid = *xid;
-
-       SpinRelease(OidGenLockId);
+       LWLockRelease(CLogTruncationLock);
 }
 
 /*
- * Like GetNewTransactionId reads nextXid but don't fetch it.
+ * Determine the last safe XID to allocate using the currently oldest
+ * datfrozenxid (ie, the oldest XID that might exist in any database
+ * of our cluster), and the OID of the (or a) database with that value.
  */
 void
-ReadNewTransactionId(TransactionId *xid)
+SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
 {
+       TransactionId xidVacLimit;
+       TransactionId xidWarnLimit;
+       TransactionId xidStopLimit;
+       TransactionId xidWrapLimit;
+       TransactionId curXid;
 
-       /* ----------------
-        *      during bootstrap initialization, we return the special
-        *      bootstrap transaction id.
-        * ----------------
-        */
-       if (AMI_OVERRIDE)
-       {
-               TransactionIdStore(AmiTransactionId, xid);
-               return;
-       }
-
-       SpinAcquire(OidGenLockId);      /* not good for concurrency... */
+       Assert(TransactionIdIsNormal(oldest_datfrozenxid));
 
        /*
-        * Note that we don't check is ShmemVariableCache->xid_count equal to
-        * 0 or not. This will work as long as we don't call
-        * ReadNewTransactionId() before GetNewTransactionId().
+        * The place where we actually get into deep trouble is halfway around
+        * from the oldest potentially-existing XID.  (This calculation is
+        * probably off by one or two counts, because the special XIDs reduce the
+        * size of the loop a little bit.  But we throw in plenty of slop below,
+        * so it doesn't matter.)
         */
-       if (ShmemVariableCache->nextXid == 0)
-               elog(ERROR, "ReadNewTransactionId: ShmemVariableCache->nextXid is not initialized");
-
-       TransactionIdStore(ShmemVariableCache->nextXid, xid);
-
-       SpinRelease(OidGenLockId);
-}
-
-/* ----------------------------------------------------------------
- *                                     object id generation support
- * ----------------------------------------------------------------
- */
+       xidWrapLimit = oldest_datfrozenxid + (MaxTransactionId >> 1);
+       if (xidWrapLimit < FirstNormalTransactionId)
+               xidWrapLimit += FirstNormalTransactionId;
 
-/* ----------------
- *             GetNewObjectIdBlock
- *
- *             This support function is used to allocate a block of object ids
- *             of the given size.
- * ----------------
- */
-static void
-GetNewObjectIdBlock(Oid *oid_return,   /* place to return the first new
-                                                                                * object id */
-                                       int oid_block_size) /* number of oids desired */
-{
-       Oid                     firstfreeoid;
-       Oid                     nextoid;
-
-       /* ----------------
-        *  Obtain exclusive access to the variable relation page
-        * ----------------
+       /*
+        * We'll refuse to continue assigning XIDs in interactive mode once we get
+        * within 1M transactions of data loss.  This leaves lots of room for the
+        * DBA to fool around fixing things in a standalone backend, while not
+        * being significant compared to total XID space. (Note that since
+        * vacuuming requires one transaction per table cleaned, we had better be
+        * sure there's lots of XIDs left...)
         */
-       SpinAcquire(OidGenLockId);
+       xidStopLimit = xidWrapLimit - 1000000;
+       if (xidStopLimit < FirstNormalTransactionId)
+               xidStopLimit -= FirstNormalTransactionId;
 
-       /* ----------------
-        *      get the "next" oid from the variable relation
-        * ----------------
+       /*
+        * We'll start complaining loudly when we get within 10M transactions of
+        * the stop point.  This is kind of arbitrary, but if you let your gas
+        * gauge get down to 1% of full, would you be looking for the next gas
+        * station?  We need to be fairly liberal about this number because there
+        * are lots of scenarios where most transactions are done by automatic
+        * clients that won't pay attention to warnings. (No, we're not gonna make
+        * this configurable.  If you know enough to configure it, you know enough
+        * to not get in this kind of trouble in the first place.)
         */
-       VariableRelationGetNextOid(&firstfreeoid);
+       xidWarnLimit = xidStopLimit - 10000000;
+       if (xidWarnLimit < FirstNormalTransactionId)
+               xidWarnLimit -= FirstNormalTransactionId;
 
-       /* ----------------
-        *      Allocate the range of OIDs to be returned to the caller.
-        *
-        *      There are two things going on here.
-        *
-        *      One: in a virgin database pg_variable will initially contain zeroes,
-        *      so we will read out firstfreeoid = InvalidOid.  We want to start
-        *      allocating OIDs at BootstrapObjectIdData instead (OIDs below that
-        *      are reserved for static assignment in the initial catalog data).
+       /*
+        * We'll start trying to force autovacuums when oldest_datfrozenxid gets
+        * to be more than autovacuum_freeze_max_age transactions old.
         *
-        *      Two: if a database is run long enough, the OID counter will wrap
-        *      around.  We must not generate an invalid OID when that happens,
-        *      and it seems wise not to generate anything in the reserved range.
-        *      Therefore we advance to BootstrapObjectIdData in this case too.
+        * Note: guc.c ensures that autovacuum_freeze_max_age is in a sane range,
+        * so that xidVacLimit will be well before xidWarnLimit.
         *
-        *      The comparison here assumes that Oid is an unsigned type.
+        * Note: autovacuum_freeze_max_age is a PGC_POSTMASTER parameter so that
+        * we don't have to worry about dealing with on-the-fly changes in its
+        * value.  It doesn't look practical to update shared state from a GUC
+        * assign hook (too many processes would try to execute the hook,
+        * resulting in race conditions as well as crashes of those not connected
+        * to shared memory).  Perhaps this can be improved someday.  See also
+        * SetMultiXactIdLimit.
         */
-       nextoid = firstfreeoid + oid_block_size;
-
-       if (! OidIsValid(firstfreeoid) || nextoid < firstfreeoid)
-       {
-               /* Initialization or wraparound time, force it up to safe range */
-               firstfreeoid = BootstrapObjectIdData;
-               nextoid = firstfreeoid + oid_block_size;
-       }
-
-       (*oid_return) = firstfreeoid;
-
-       /* ----------------
-        *      Update the variable relation to show the block range as used.
-        * ----------------
-        */
-       VariableRelationPutNextOid(nextoid);
-
-       /* ----------------
-        *      Relinquish our lock on the variable relation page
-        * ----------------
-        */
-       SpinRelease(OidGenLockId);
-}
-
-/* ----------------
- *             GetNewObjectId
- *
- *             This function allocates and parses out object ids.      Like
- *             GetNewTransactionId(), it "prefetches" 32 object ids by
- *             incrementing the nextOid stored in the var relation by 32 and then
- *             returning these id's one at a time until they are exhausted.
- *             This means we reduce the number of accesses to the variable
- *             relation by 32 for each backend.
- *
- *             Note:  32 has no special significance.  We don't want the
- *                        number to be too large because when the backend
- *                        terminates, we lose the oids we cached.
- *
- *             Question: couldn't we use a shared-memory cache just like XIDs?
- *             That would allow a larger interval between pg_variable updates
- *             without cache losses.  Note, however, that we can assign an OID
- *             without even a spinlock from the backend-local OID cache.
- *             Maybe two levels of caching would be good.
- * ----------------
- */
-
-#define VAR_OID_PREFETCH               32
+       xidVacLimit = oldest_datfrozenxid + autovacuum_freeze_max_age;
+       if (xidVacLimit < FirstNormalTransactionId)
+               xidVacLimit += FirstNormalTransactionId;
+
+       /* Grab lock for just long enough to set the new limit values */
+       LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
+       ShmemVariableCache->oldestXid = oldest_datfrozenxid;
+       ShmemVariableCache->xidVacLimit = xidVacLimit;
+       ShmemVariableCache->xidWarnLimit = xidWarnLimit;
+       ShmemVariableCache->xidStopLimit = xidStopLimit;
+       ShmemVariableCache->xidWrapLimit = xidWrapLimit;
+       ShmemVariableCache->oldestXidDB = oldest_datoid;
+       curXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
+       LWLockRelease(XidGenLock);
+
+       /* Log the info */
+       ereport(DEBUG1,
+                       (errmsg("transaction ID wrap limit is %u, limited by database with OID %u",
+                                       xidWrapLimit, oldest_datoid)));
 
-static int     prefetched_oid_count = 0;
-static Oid     next_prefetched_oid;
-
-void
-GetNewObjectId(Oid *oid_return) /* place to return the new object id */
-{
-       /* ----------------
-        *      if we run out of prefetched oids, then we get some
-        *      more before handing them out to the caller.
-        * ----------------
+       /*
+        * If past the autovacuum force point, immediately signal an autovac
+        * request.  The reason for this is that autovac only processes one
+        * database per invocation.  Once it's finished cleaning up the oldest
+        * database, it'll call here, and we'll signal the postmaster to start
+        * another iteration immediately if there are still any old databases.
         */
+       if (TransactionIdFollowsOrEquals(curXid, xidVacLimit) &&
+               IsUnderPostmaster && !InRecovery)
+               SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
 
-       if (prefetched_oid_count == 0)
+       /* Give an immediate warning if past the wrap warn point */
+       if (TransactionIdFollowsOrEquals(curXid, xidWarnLimit) && !InRecovery)
        {
-               int                     oid_block_size = VAR_OID_PREFETCH;
-
-               /* ----------------
-                *              Make sure pg_variable is open.
-                * ----------------
+               char       *oldest_datname;
+
+               /*
+                * We can be called when not inside a transaction, for example during
+                * StartupXLOG().  In such a case we cannot do database access, so we
+                * must just report the oldest DB's OID.
+                *
+                * Note: it's also possible that get_database_name fails and returns
+                * NULL, for example because the database just got dropped.  We'll
+                * still warn, even though the warning might now be unnecessary.
                 */
-               if (!RelationIsValid(VariableRelation))
-                       VariableRelation = heap_openr(VariableRelationName, NoLock);
-
-               /* ----------------
-                *              get a new block of prefetched object ids.
-                * ----------------
-                */
-               GetNewObjectIdBlock(&next_prefetched_oid, oid_block_size);
-
-               /* ----------------
-                *              now reset the prefetched_oid_count.
-                * ----------------
-                */
-               prefetched_oid_count = oid_block_size;
+               if (IsTransactionState())
+                       oldest_datname = get_database_name(oldest_datoid);
+               else
+                       oldest_datname = NULL;
+
+               if (oldest_datname)
+                       ereport(WARNING,
+                                       (errmsg("database \"%s\" must be vacuumed within %u transactions",
+                                                       oldest_datname,
+                                                       xidWrapLimit - curXid),
+                                        errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
+                                                        "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
+               else
+                       ereport(WARNING,
+                                       (errmsg("database with OID %u must be vacuumed within %u transactions",
+                                                       oldest_datoid,
+                                                       xidWrapLimit - curXid),
+                                        errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
+                                                        "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
        }
+}
 
-       /* ----------------
-        *      return the next prefetched oid in the pointer passed by
-        *      the user and decrement the prefetch count.
-        * ----------------
-        */
-       if (PointerIsValid(oid_return))
-               (*oid_return) = next_prefetched_oid;
 
-       next_prefetched_oid++;
-       prefetched_oid_count--;
+/*
+ * ForceTransactionIdLimitUpdate -- does the XID wrap-limit data need updating?
+ *
+ * We primarily check whether oldestXidDB is valid.  The cases we have in
+ * mind are that that database was dropped, or the field was reset to zero
+ * by pg_resetwal.  In either case we should force recalculation of the
+ * wrap limit.  Also do it if oldestXid is old enough to be forcing
+ * autovacuums or other actions; this ensures we update our state as soon
+ * as possible once extra overhead is being incurred.
+ */
+bool
+ForceTransactionIdLimitUpdate(void)
+{
+       TransactionId nextXid;
+       TransactionId xidVacLimit;
+       TransactionId oldestXid;
+       Oid                     oldestXidDB;
+
+       /* Locking is probably not really necessary, but let's be careful */
+       LWLockAcquire(XidGenLock, LW_SHARED);
+       nextXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
+       xidVacLimit = ShmemVariableCache->xidVacLimit;
+       oldestXid = ShmemVariableCache->oldestXid;
+       oldestXidDB = ShmemVariableCache->oldestXidDB;
+       LWLockRelease(XidGenLock);
+
+       if (!TransactionIdIsNormal(oldestXid))
+               return true;                    /* shouldn't happen, but just in case */
+       if (!TransactionIdIsValid(xidVacLimit))
+               return true;                    /* this shouldn't happen anymore either */
+       if (TransactionIdFollowsOrEquals(nextXid, xidVacLimit))
+               return true;                    /* past xidVacLimit, don't delay updating */
+       if (!SearchSysCacheExists1(DATABASEOID, ObjectIdGetDatum(oldestXidDB)))
+               return true;                    /* could happen, per comments above */
+       return false;
 }
 
-void
-CheckMaxObjectId(Oid assigned_oid)
-{
-       Oid                     temp_oid;
 
-       if (prefetched_oid_count == 0)          /* make sure next/max is set, or
-                                                                                * reload */
-               GetNewObjectId(&temp_oid);
+/*
+ * GetNewObjectId -- allocate a new OID
+ *
+ * OIDs are generated by a cluster-wide counter.  Since they are only 32 bits
+ * wide, counter wraparound will occur eventually, and therefore it is unwise
+ * to assume they are unique unless precautions are taken to make them so.
+ * Hence, this routine should generally not be used directly.  The only direct
+ * callers should be GetNewOidWithIndex() and GetNewRelFileNode() in
+ * catalog/catalog.c.
+ */
+Oid
+GetNewObjectId(void)
+{
+       Oid                     result;
 
-       /* ----------------
-        *      If we are below prefetched limits, do nothing
-        * ----------------
-        */
+       /* safety check, we should never get this far in a HS standby */
+       if (RecoveryInProgress())
+               elog(ERROR, "cannot assign OIDs during recovery");
 
-       if (assigned_oid < next_prefetched_oid)
-               return;
+       LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
 
-       /* ----------------
-        *      If we are here, we are coming from a 'copy from' with oid's
+       /*
+        * Check for wraparound of the OID counter.  We *must* not return 0
+        * (InvalidOid), and in normal operation we mustn't return anything below
+        * FirstNormalObjectId since that range is reserved for initdb (see
+        * IsCatalogRelationOid()).  Note we are relying on unsigned comparison.
         *
-        *      If we are in the prefetched oid range, just bump it up
-        * ----------------
+        * During initdb, we start the OID generator at FirstBootstrapObjectId, so
+        * we only wrap if before 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) FirstNormalObjectId))
+       {
+               if (IsPostmasterEnvironment)
+               {
+                       /* wraparound, or first post-initdb assignment, in normal mode */
+                       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 mode (unlikely but possible) */
+                               ShmemVariableCache->nextOid = FirstNormalObjectId;
+                               ShmemVariableCache->oidCount = 0;
+                       }
+               }
+       }
 
-       if (assigned_oid <= next_prefetched_oid + prefetched_oid_count - 1)
+       /* If we run out of logged for use oids then we must log more */
+       if (ShmemVariableCache->oidCount == 0)
        {
-               prefetched_oid_count -= assigned_oid - next_prefetched_oid + 1;
-               next_prefetched_oid = assigned_oid + 1;
-               return;
+               XLogPutNextOid(ShmemVariableCache->nextOid + VAR_OID_PREFETCH);
+               ShmemVariableCache->oidCount = VAR_OID_PREFETCH;
        }
 
-       /* ----------------
-        *      We have exceeded the prefetch oid range
-        *
-        *      We should lock the database and kill all other backends
-        *      but we are loading oid's that we can not guarantee are unique
-        *      anyway, so we must rely on the user
-        *
-        * We now:
-        *        set the variable relation with the new max oid
-        *        force the backend to reload its oid cache
-        *
-        * By reloading the oid cache, we don't have to update the variable
-        * relation every time when sequential OIDs are being loaded by COPY.
-        * ----------------
-        */
+       result = ShmemVariableCache->nextOid;
 
-       SpinAcquire(OidGenLockId);
-       VariableRelationPutNextOid(assigned_oid);
-       SpinRelease(OidGenLockId);
+       (ShmemVariableCache->nextOid)++;
+       (ShmemVariableCache->oidCount)--;
 
-       prefetched_oid_count = 0;       /* force reload */
-       GetNewObjectId(&temp_oid);      /* cause target OID to be allocated */
-}
+       LWLockRelease(OidGenLock);
 
-#endif /* !XLOG */
+       return result;
+}